Handler.php 4.4 KB

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