Update Exception handler to report validation message bag errors

This commit is contained in:
Daniel Supernault 2022-02-05 18:37:46 -07:00
parent b1fe0e8b3a
commit 74905ba1d0
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
1 changed files with 10 additions and 1 deletions

View File

@ -68,11 +68,20 @@ class Handler extends ExceptionHandler
*/
public function render($request, Throwable $exception)
{
if ($request->wantsJson())
if ($exception instanceof \Illuminate\Validation\ValidationException && $request->wantsJson()) {
return response()->json(
[
'message' => $exception->getMessage(),
'errors' => $exception->validator->getMessageBag()
],
method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500
);
} else if ($request->wantsJson()) {
return response()->json(
['error' => $exception->getMessage()],
method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500
);
}
return parent::render($request, $exception);
}
}