Handler.php 1.6 KB

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