Handler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Http\Request;
  8. use Illuminate\Session\TokenMismatchException;
  9. use Log;
  10. use ReflectionException;
  11. use Response;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. class Handler extends ExceptionHandler {
  14. /**
  15. * A list of the exception types that are not reported.
  16. *
  17. * @var array
  18. */
  19. protected $dontReport = [];
  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. Log::info('异常来源:'.get_class($exception));
  40. // 调试模式下记录错误详情
  41. if(config('app.debug')){
  42. Log::debug('来自链接:'.url()->full());
  43. Log::debug($exception);
  44. }
  45. parent::report($exception);
  46. }
  47. /**
  48. * Render an exception into an HTTP response.
  49. *
  50. * @param Request $request
  51. * @param Exception $exception
  52. *
  53. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  54. */
  55. public function render($request, Exception $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')]);
  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')]);
  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. ]);
  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')]);
  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. ]);
  105. }
  106. return parent::render($request, $exception);
  107. }
  108. }