Handler.php 3.4 KB

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