Handler.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\TokenInvalidException;
  10. class Handler extends ExceptionHandler
  11. {
  12. /**
  13. * A list of the exception types that should not be reported.
  14. *
  15. * @var array
  16. */
  17. protected $dontReport = [
  18. AuthorizationException::class,
  19. HttpException::class,
  20. ModelNotFoundException::class,
  21. ValidationException::class,
  22. ];
  23. /**
  24. * Report or log an exception.
  25. *
  26. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  27. *
  28. * @param \Exception $exception
  29. * @return void
  30. */
  31. public function report(Exception $exception)
  32. {
  33. parent::report($exception);
  34. }
  35. /**
  36. * Render an exception into an HTTP response.
  37. *
  38. * @param \Illuminate\Http\Request $request
  39. * @param \Exception $exception
  40. * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  41. */
  42. public function render($request, Exception $exception)
  43. {
  44. if($exception instanceof TokenInvalidException){
  45. $error = [
  46. 'message' => $exception->getMessage(),
  47. 'code' => 401,
  48. ];
  49. return response()->json($error)->setStatusCode(401);
  50. }
  51. return parent::render($request, $exception);
  52. }
  53. }