Asegurar que los errores retornen un Json
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Exceptions;
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Auth\Access\AuthorizationException;
|
use Illuminate\Auth\Access\AuthorizationException;
|
||||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
@@ -22,6 +23,15 @@ class Handler extends ExceptionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function render($request, Throwable $exception) {
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user