Handler.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. $errMsg='';
  44. try{
  45. if (substr($exception->getFile(),-(strlen("FatalError.php")))==='FatalError.php'){
  46. \Illuminate\Support\Facades\Log::emergency("FPM内存耗尽",["user"=>Auth::id(),"param"=>$request->all()]);
  47. return response()->view('exception.default',[
  48. 'code'=> "500",
  49. 'message'=>"网站负载过大,联系管理员处理",
  50. ]);
  51. }
  52. $type = $exception->type ?? 'error';
  53. $errMsg=(function()use($request,$exception){
  54. return $errMsg=
  55. '异常: '
  56. .' URL:'.(
  57. method_exists($request,'fullUrl')
  58. ?($request->fullUrl()??'')
  59. :'')
  60. .' method:'.(
  61. method_exists($request,'method')
  62. ?($request->method()??'')
  63. :'')
  64. .' params:'.(
  65. method_exists($request,'all')
  66. ?json_encode($request->all()??'')
  67. :'')
  68. .' errors:'.(
  69. method_exists($exception,'errors')
  70. ?(json_encode($exception->errors(),JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)??'')
  71. :'')
  72. .' header:'.(
  73. method_exists($request,'header')
  74. ?json_encode($request->headers->all()??'')
  75. :'')
  76. .' code:'.(
  77. method_exists($exception,'getStatusCode')
  78. ?($exception->getStatusCode()??'')
  79. :'')
  80. .' message:'.(
  81. method_exists($exception,'getMessage')
  82. ?($exception->getMessage()??'')
  83. :'')
  84. .' trace:'.
  85. (method_exists($exception,'getTraceAsString')
  86. ?($exception->getTraceAsString()??'')
  87. :'');
  88. })();
  89. if($request->is("api/*")
  90. && $exception instanceof ValidationException){
  91. return response()->json($exception->errors(),200,[],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  92. }
  93. if(strpos($exception->getMessage(),'This action is unauthorized')!==false){
  94. return response()->view('exception.unauthorized');
  95. }
  96. if (method_exists($exception,'getStatusCode')) {
  97. $code = $exception->getStatusCode();
  98. switch ($code){
  99. case 419: $view='exception.login';break;
  100. case 404:$view='exception.404';break;
  101. default: $view='exception.default';break;
  102. }
  103. return response()->view($view,[
  104. 'code'=> $code,
  105. 'message'=>$exception->getMessage(),
  106. ]);
  107. }
  108. }catch (\Exception $e){}finally{
  109. list(
  110. $className,
  111. $functionName,
  112. $tracesAll
  113. ) =(function()use($exception){
  114. $traces=method_exists($exception,'getTraceAsString')
  115. ?($exception->getTraceAsString()??'')
  116. :'';
  117. if(!$traces)return '';
  118. preg_match('/\#0.*?\: (.*?)(-\>|\:\:)(.*?)\(/', $traces, $result);
  119. return [$result[1]??'',$result[3]??'',$traces];
  120. })();
  121. LogService::log(
  122. $className,
  123. $functionName,
  124. ($errMsg??'')
  125. .'请求:'.json_encode($request->all())
  126. .'调用堆栈r:'.$tracesAll,
  127. Auth::id()??'',
  128. $type
  129. );
  130. }
  131. return parent::render($request, $exception);
  132. }
  133. }