|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Exceptions; |
| 4 | + |
| 5 | +use http\Exception; |
| 6 | +use Illuminate\Auth\Access\AuthorizationException; |
| 7 | +use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 8 | +use Illuminate\Validation\ValidationException; |
| 9 | +use Laravel\Lumen\Exceptions\Handler as ExceptionHandler; |
| 10 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 11 | +use Throwable; |
| 12 | + |
| 13 | +class Handler extends ExceptionHandler |
| 14 | +{ |
| 15 | + |
| 16 | + /** |
| 17 | + * A list of the exception types that should not be reported. |
| 18 | + * |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + protected $dontReport = [ |
| 22 | + AuthorizationException::class, |
| 23 | + HttpException::class, |
| 24 | + ModelNotFoundException::class, |
| 25 | + ValidationException::class, |
| 26 | + ]; |
| 27 | + |
| 28 | + /** |
| 29 | + * Report or log an exception. |
| 30 | + * |
| 31 | + * This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
| 32 | + * |
| 33 | + * @param \Throwable $exception |
| 34 | + * |
| 35 | + * @return void |
| 36 | + * |
| 37 | + * @throws \Exception |
| 38 | + */ |
| 39 | + public function report(Throwable $exception) |
| 40 | + { |
| 41 | + |
| 42 | + parent::report($exception); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Render an exception into an HTTP response. |
| 47 | + * |
| 48 | + * @param \Illuminate\Http\Request $request |
| 49 | + * @param \Throwable $exception |
| 50 | + * |
| 51 | + * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse |
| 52 | + * |
| 53 | + * @throws \Throwable |
| 54 | + */ |
| 55 | + public function render($request, Exception $e) |
| 56 | + { |
| 57 | + |
| 58 | + $rendered = parent::render($request, $e); |
| 59 | + |
| 60 | + return response()->json([ |
| 61 | + 'error' => [ |
| 62 | + 'code' => $rendered->getStatusCode(), |
| 63 | + 'message' => $e->getMessage(), |
| 64 | + ] |
| 65 | + ], $rendered->getStatusCode()); |
| 66 | + } |
| 67 | +} |
0 commit comments