Handler.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Log;
  4. use App\Services\LogService;
  5. use Exception;
  6. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Validation\ValidationException;
  9. use Symfony\Component\ErrorHandler\Error\FatalError;
  10. use Throwable;
  11. class Handler extends ExceptionHandler
  12. {
  13. /**
  14. * A list of the exception types that are not reported.
  15. *
  16. * @var array
  17. */
  18. protected $dontReport = [
  19. //
  20. ];
  21. /**
  22. * A list of the inputs that are never flashed for validation exceptions.
  23. *
  24. * @var array
  25. */
  26. protected $dontFlash = [
  27. 'password',
  28. 'password_confirmation',
  29. ];
  30. /**
  31. * Report or log an exception.
  32. *
  33. * @param Throwable $exception
  34. * @return void
  35. * @throws Exception|Throwable
  36. */
  37. public function report(Throwable $exception)
  38. {
  39. parent::report($exception);
  40. }
  41. public function render( $request, Throwable $exception)
  42. {
  43. \Illuminate\Support\Facades\Log::info("抓取内存耗尽错误",debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT,5));
  44. $errMsg='';
  45. try{
  46. if (substr($exception->getFile(),-(strlen("FatalError.php")))==='FatalError.php'){
  47. \Illuminate\Support\Facades\Log::emergency("FPM内存耗尽",["user"=>Auth::id(),"param"=>$request->all()]);
  48. return response()->view('exception.default',[
  49. 'code'=> "500",
  50. 'message'=>"网站负载过大,联系管理员处理",
  51. ]);
  52. }
  53. $type = $exception->type ?? 'error';
  54. $errMsg=(function()use($request,$exception){
  55. return $errMsg=
  56. '异常: '
  57. .' URL:'.(
  58. method_exists($request,'fullUrl')
  59. ?($request->fullUrl()??'')
  60. :'')
  61. .' method:'.(
  62. method_exists($request,'method')
  63. ?($request->method()??'')
  64. :'')
  65. .' params:'.(
  66. method_exists($request,'all')
  67. ?json_encode($request->all()??'')
  68. :'')
  69. .' errors:'.(
  70. method_exists($exception,'errors')
  71. ?(json_encode($exception->errors(),JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)??'')
  72. :'')
  73. .' header:'.(
  74. method_exists($request,'header')
  75. ?json_encode($request->headers->all()??'')
  76. :'')
  77. .' code:'.(
  78. method_exists($exception,'getStatusCode')
  79. ?($exception->getStatusCode()??'')
  80. :'')
  81. .' message:'.(
  82. method_exists($exception,'getMessage')
  83. ?($exception->getMessage()??'')
  84. :'')
  85. .' trace:'.
  86. (method_exists($exception,'getTraceAsString')
  87. ?($exception->getTraceAsString()??'')
  88. :'');
  89. })();
  90. if($request->is("api/*")
  91. && $exception instanceof ValidationException){
  92. return response()->json($exception->errors(),200,[],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  93. }
  94. if(strpos($exception->getMessage(),'This action is unauthorized')!==false){
  95. return response()->view('exception.unauthorized');
  96. }
  97. if (method_exists($exception,'getStatusCode')) {
  98. $code = $exception->getStatusCode();
  99. switch ($code){
  100. case 419: $view='exception.login';break;
  101. case 404:$view='exception.404';break;
  102. default: $view='exception.default';break;
  103. }
  104. return response()->view($view,[
  105. 'code'=> $code,
  106. 'message'=>$exception->getMessage(),
  107. ]);
  108. }
  109. }catch (\Exception $e){}finally{
  110. list(
  111. $className,
  112. $functionName,
  113. $tracesAll
  114. ) =(function()use($exception){
  115. $traces=method_exists($exception,'getTraceAsString')
  116. ?($exception->getTraceAsString()??'')
  117. :'';
  118. if(!$traces)return '';
  119. preg_match('/\#0.*?\: (.*?)(-\>|\:\:)(.*?)\(/', $traces, $result);
  120. return [$result[1]??'',$result[3]??'',$traces];
  121. })();
  122. LogService::log(
  123. $className,
  124. $functionName,
  125. ($errMsg??'')
  126. .'请求:'.json_encode($request->all())
  127. .'调用堆栈r:'.$tracesAll,
  128. Auth::id()??'',
  129. $type
  130. );
  131. }
  132. return parent::render($request, $exception);
  133. }
  134. }