Handler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Session\TokenMismatchException;
  7. use ReflectionException;
  8. class Handler extends ExceptionHandler
  9. {
  10. /**
  11. * A list of the exception types that should not be reported.
  12. *
  13. * @var array
  14. */
  15. protected $dontReport = [
  16. \Illuminate\Auth\AuthenticationException::class,
  17. \Illuminate\Auth\Access\AuthorizationException::class,
  18. \Symfony\Component\HttpKernel\Exception\HttpException::class,
  19. \Illuminate\Database\Eloquent\ModelNotFoundException::class,
  20. \Illuminate\Session\TokenMismatchException::class,
  21. \Illuminate\Validation\ValidationException::class,
  22. ];
  23. /**
  24. * Report or log an exception.
  25. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  26. *
  27. * @param Exception $exception
  28. *
  29. * @throws Exception
  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. *
  41. * @return \Illuminate\Http\Response
  42. */
  43. public function render($request, Exception $exception)
  44. {
  45. \Log::info("异常请求:" . $request->fullUrl() . ",IP:" . getClientIp());
  46. // 调试模式下直接返回错误信息
  47. if (config('app.debug')) {
  48. return parent::render($request, $exception);
  49. }
  50. // 捕获身份校验异常
  51. if ($exception instanceof AuthenticationException) {
  52. if ($request->ajax()) {
  53. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'Unauthorized']);
  54. } else {
  55. return response()->view('error.404');
  56. }
  57. }
  58. // 捕获CSRF异常
  59. if ($exception instanceof TokenMismatchException) {
  60. if ($request->ajax()) {
  61. return response()->json(['status' => 'fail', 'data' => '', 'message' => trans('404.csrf_title')]);
  62. } else {
  63. return response()->view('error.csrf');
  64. }
  65. }
  66. // 捕获反射异常
  67. if ($exception instanceof ReflectionException) {
  68. if ($request->ajax()) {
  69. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'System Error']);
  70. } else {
  71. return response()->view('error.404');
  72. }
  73. }
  74. return response()->view('error.404');
  75. }
  76. /**
  77. * Convert an authentication exception into an unauthenticated response.
  78. *
  79. * @param \Illuminate\Http\Request $request
  80. * @param \Illuminate\Auth\AuthenticationException $exception
  81. *
  82. * @return \Illuminate\Http\Response
  83. */
  84. protected function unauthenticated($request, AuthenticationException $exception)
  85. {
  86. if ($request->expectsJson()) {
  87. return response()->json(['error' => 'Unauthenticated.'], 401);
  88. }
  89. return redirect()->guest(route('login'));
  90. }
  91. }