Handler.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Exceptions;
  3. use Exception;
  4. use Illuminate\Auth\AuthenticationException;
  5. use Illuminate\Validation\ValidationException;
  6. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  7. class Handler extends ExceptionHandler
  8. {
  9. /**
  10. * A list of the exception types that are not reported.
  11. *
  12. * @var array
  13. */
  14. protected $dontReport = [
  15. //
  16. ];
  17. /**
  18. * A list of the inputs that are never flashed for validation exceptions.
  19. *
  20. * @var array
  21. */
  22. protected $dontFlash = [
  23. 'password',
  24. 'password_confirmation',
  25. ];
  26. /**
  27. * Report or log an exception.
  28. *
  29. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  30. *
  31. * @param \Exception $exception
  32. * @return void
  33. */
  34. public function report(Exception $exception)
  35. {
  36. parent::report($exception);
  37. }
  38. /**
  39. * Render an exception into an HTTP response.
  40. *
  41. * @param \Illuminate\Http\Request $request
  42. * @param \Exception $exception
  43. * @return \Illuminate\Http\Response
  44. */
  45. public function render($request, Exception $exception)
  46. {
  47. return parent::render($request, $exception);
  48. }
  49. /**
  50. * Convert a validation exception into a JSON response.
  51. *
  52. * @param \Illuminate\Http\Request $request
  53. * @param \Illuminate\Validation\ValidationException $exception
  54. * @return \Illuminate\Http\JsonResponse
  55. */
  56. protected function invalidJson($request, ValidationException $exception)
  57. {
  58. return response()->json([
  59. 'message' => $exception->getMessage(),
  60. 'errors' => $exception->errors(),
  61. ], $exception->status);
  62. }
  63. /**
  64. * Convert an authentication exception into a response.
  65. *
  66. * @param \Illuminate\Http\Request $request
  67. * @param \Illuminate\Auth\AuthenticationException $exception
  68. * @return \Illuminate\Http\Response
  69. */
  70. protected function unauthenticated($request, AuthenticationException $exception)
  71. {
  72. return $request->expectsJson()
  73. ? response()->json(['message' => 'Unauthenticated.'], 401)
  74. : redirect()->guest(route('login'));
  75. }
  76. }