Handler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // 调试模式下记录错误详情
  42. if (config('app.debug')) {
  43. \Log::info($exception);
  44. }
  45. parent::report($exception);
  46. }
  47. /**
  48. * Render an exception into an HTTP response.
  49. *
  50. * @param \Illuminate\Http\Request $request
  51. * @param \Exception $exception
  52. *
  53. * @return \Illuminate\Http\Response
  54. */
  55. public function render($request, Exception $exception)
  56. {
  57. // 调试模式下直接返回错误信息
  58. if (config('app.debug')) {
  59. return parent::render($request, $exception);
  60. }
  61. // 捕获访问异常
  62. if ($exception instanceof NotFoundHttpException) {
  63. \Log::info("异常请求:" . $request->fullUrl() . ",IP:" . getClientIp());
  64. if ($request->ajax()) {
  65. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'Page Not Found']);
  66. } else {
  67. return response()->view('auth.error', ['message' => trans('error.MissingPage')]);
  68. }
  69. }
  70. // 捕获身份校验异常
  71. if ($exception instanceof AuthenticationException) {
  72. if ($request->ajax()) {
  73. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'Unauthorized']);
  74. } else {
  75. return response()->view('auth.error', ['message' => trans('error.Unauthorized')]);
  76. }
  77. }
  78. // 捕获CSRF异常
  79. if ($exception instanceof TokenMismatchException) {
  80. if ($request->ajax()) {
  81. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'System Error, Please Refresh Page, Try One More Time']);
  82. } else {
  83. return response()->view('auth.error', ['message' => trans('error.RefreshPage')]);
  84. }
  85. }
  86. // 捕获反射异常
  87. if ($exception instanceof ReflectionException) {
  88. if ($request->ajax()) {
  89. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'System Error']);
  90. } else {
  91. return response()->view('auth.error', ['message' => trans('error.SystemError')]);
  92. }
  93. }
  94. // 捕获系统错误异常
  95. if ($exception instanceof ErrorException) {
  96. if ($request->ajax()) {
  97. return response()->json(['status' => 'fail', 'data' => '', 'message' => 'System Error']);
  98. } else {
  99. return response()->view('auth.error', ['message' => trans('error.SystemError') . ', ' . trans('error.Visit') . '<a href="/logs" target="_blank">' . trans('error.log') . '</a>']);
  100. }
  101. }
  102. return parent::render($request, $exception);
  103. }
  104. }