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

View file

@ -68,11 +68,20 @@ class Handler extends ExceptionHandler
*/ */
public function render($request, Throwable $exception) 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( return response()->json(
['error' => $exception->getMessage()], ['error' => $exception->getMessage()],
method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500 method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500
); );
}
return parent::render($request, $exception); return parent::render($request, $exception);
} }
} }