Handler.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Exceptions;
  3. use Exception;
  4. use Illuminate\Auth\AuthenticationException;
  5. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  6. use Illuminate\Support\Facades\Log;
  7. use Symfony\Component\HttpKernel\Exception\HttpException;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. /**
  10. * @codeCoverageIgnore
  11. */
  12. class Handler extends ExceptionHandler
  13. {
  14. /**
  15. * A list of the exception types that should not be reported.
  16. *
  17. * @var array
  18. */
  19. protected $dontReport = [
  20. \Illuminate\Auth\AuthenticationException::class,
  21. \Illuminate\Auth\Access\AuthorizationException::class,
  22. \Symfony\Component\HttpKernel\Exception\HttpException::class,
  23. \Illuminate\Database\Eloquent\ModelNotFoundException::class,
  24. \Illuminate\Session\TokenMismatchException::class,
  25. \Illuminate\Validation\ValidationException::class,
  26. ];
  27. /**
  28. * Report or log an exception.
  29. *
  30. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  31. *
  32. * @param \Exception $exception
  33. * @return void
  34. */
  35. public function report(Exception $exception)
  36. {
  37. parent::report($exception);
  38. }
  39. /**
  40. * Render an exception into an HTTP response.
  41. *
  42. * @param \Illuminate\Http\Request $request
  43. * @param \Exception $exception
  44. * @return \Illuminate\Http\Response
  45. */
  46. public function render($request, Exception $exception)
  47. {
  48. if ($exception instanceof NotFoundHttpException) {
  49. return response()->view('layouts.errors.404', [], 404);
  50. } elseif ($exception instanceof HttpException && $exception->getStatusCode() == 403) {
  51. return response()->view(
  52. 'layouts.errors.403',
  53. ['error' => 'Sorry, this page is restricted to authorized users only.'],
  54. 403
  55. );
  56. } elseif ($exception instanceof HttpException) {
  57. Log::info($exception->getMessage());
  58. return response()->view('layouts.errors.503', ['error' => $exception->getTrace()], 500);
  59. }
  60. return parent::render($request, $exception);
  61. }
  62. /**
  63. * Convert an authentication exception into an unauthenticated response.
  64. *
  65. * @param \Illuminate\Http\Request $request
  66. * @param \Illuminate\Auth\AuthenticationException $exception
  67. * @return \Illuminate\Http\Response
  68. */
  69. protected function unauthenticated($request, AuthenticationException $exception)
  70. {
  71. if ($request->expectsJson()) {
  72. return response()->json(['error' => 'Unauthenticated.'], 401);
  73. }
  74. return redirect()->guest(route('login'));
  75. }
  76. }