Handler.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 should not be reported.
  11. *
  12. * @var array
  13. */
  14. protected $dontReport = [
  15. //
  16. ];
  17. /**
  18. * Report or log an exception.
  19. *
  20. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  21. *
  22. * @param \Exception $exception
  23. * @return void
  24. */
  25. public function report(Exception $exception)
  26. {
  27. parent::report($exception);
  28. }
  29. /**
  30. * Render an exception into an HTTP response.
  31. *
  32. * @param \Illuminate\Http\Request $request
  33. * @param \Exception $exception
  34. * @return \Illuminate\Http\Response
  35. */
  36. public function render($request, Exception $exception)
  37. {
  38. return parent::render($request, $exception);
  39. }
  40. /**
  41. * Convert a validation exception into a response.
  42. *
  43. * @param \Illuminate\Http\Request $request
  44. * @param \Illuminate\Validation\ValidationException $exception
  45. * @return \Illuminate\Http\Response
  46. */
  47. protected function invalid($request, ValidationException $exception)
  48. {
  49. $message = $exception->getMessage();
  50. $errors = $exception->validator->errors()->messages();
  51. return $request->expectsJson()
  52. ? response()->json(['message' => $message, 'errors' => $errors], 422)
  53. : redirect()->back()->withInput()->withErrors(
  54. $errors, $exception->errorBag
  55. );
  56. }
  57. /**
  58. * Convert an authentication exception into a response.
  59. *
  60. * @param \Illuminate\Http\Request $request
  61. * @param \Illuminate\Auth\AuthenticationException $exception
  62. * @return \Illuminate\Http\Response
  63. */
  64. protected function unauthenticated($request, AuthenticationException $exception)
  65. {
  66. return $request->expectsJson()
  67. ? response()->json(['message' => 'Unauthenticated.'], 401)
  68. : redirect()->guest(route('login'));
  69. }
  70. }