Handler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Exceptions;
  3. use ErrorException;
  4. use Exception;
  5. use Illuminate\Auth\AuthenticationException;
  6. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  7. use Illuminate\Session\TokenMismatchException;
  8. use ReflectionException;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. class Handler extends ExceptionHandler
  11. {
  12. /**
  13. * A list of the exception types that are not reported.
  14. *
  15. * @var array
  16. */
  17. protected $dontReport = [
  18. //
  19. ];
  20. /**
  21. * A list of the inputs that are never flashed for validation exceptions.
  22. *
  23. * @var array
  24. */
  25. protected $dontFlash = [
  26. 'password',
  27. 'password_confirmation',
  28. ];
  29. /**
  30. * Report or log an exception.
  31. *
  32. * @param Exception $exception
  33. *
  34. * @return mixed|void
  35. * @throws Exception
  36. */
  37. public function report(Exception $exception)
  38. {
  39. // 记录异常来源
  40. \Log::info('异常来源:' . get_class($exception));
  41. parent::report($exception);
  42. }
  43. /**
  44. * Render an exception into an HTTP response.
  45. *
  46. * @param \Illuminate\Http\Request $request
  47. * @param \Exception $exception
  48. *
  49. * @return \Illuminate\Http\Response
  50. */
  51. public function render($request, Exception $exception)
  52. {
  53. // 调试模式下直接返回错误信息
  54. if (config('app.debug')) {
  55. return parent::render($request, $exception);
  56. }
  57. // 捕获访问异常
  58. if ($exception instanceof NotFoundHttpException) {
  59. \Log::info("异常请求:" . $request->fullUrl() . ",IP:" . getClientIp());
  60. if ($request->ajax()) {
  61. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'Page Not Found']);
  62. } else {
  63. return response()->view('auth.error', ['message' => 'Page Not Found']);
  64. }
  65. }
  66. // 捕获身份校验异常
  67. if ($exception instanceof AuthenticationException) {
  68. if ($request->ajax()) {
  69. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'Unauthorized']);
  70. } else {
  71. return response()->view('auth.error', ['message' => 'Unauthorized']);
  72. }
  73. }
  74. // 捕获CSRF异常
  75. if ($exception instanceof TokenMismatchException) {
  76. if ($request->ajax()) {
  77. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'Refresh Page, Try One More Time']);
  78. } else {
  79. return response()->view('auth.error', ['message' => 'Refresh Page, Try One More Time']);
  80. }
  81. }
  82. // 捕获反射异常
  83. if ($exception instanceof ReflectionException) {
  84. if ($request->ajax()) {
  85. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'System Error']);
  86. } else {
  87. return response()->view('auth.error', ['message' => 'System Error']);
  88. }
  89. }
  90. // 捕获系统错误异常
  91. if ($exception instanceof ErrorException) {
  92. if ($request->ajax()) {
  93. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'System Error']);
  94. } else {
  95. return response()->view('auth.error', ['message' => 'System Error, See <a href="/logs" target="_blank">Logs</a>']);
  96. }
  97. }
  98. return parent::render($request, $exception);
  99. }
  100. }