Asegurar que los errores retornen un Json

This commit is contained in:
2021-06-09 17:52:57 -04:00
parent dbb80a9d4e
commit 6583e8faaf

View File

@@ -2,6 +2,7 @@
namespace App\Exceptions;
use Illuminate\Http\JsonResponse;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Validation\ValidationException;
@@ -22,6 +23,15 @@ class Handler extends ExceptionHandler {
}
public function render($request, Throwable $exception) {
return parent::render($request, $exception);
$rendered = parent::render($request, $exception);
if($rendered instanceof JsonResponse) {
return $rendered;
}
return response()->json([
'error' => $rendered->getStatusCode(),
'message' => $exception instanceof HttpException ? $exception->getMessage() : 'Server Error',
], $rendered->getStatusCode());
}
}