Handler.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Services\LogService;
  4. use Exception;
  5. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Validation\ValidationException;
  8. use Throwable;
  9. class Handler extends ExceptionHandler
  10. {
  11. /**
  12. * A list of the exception types that are not reported.
  13. *
  14. * @var array
  15. */
  16. protected $dontReport = [
  17. //
  18. ];
  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. * @throws Exception
  34. */
  35. public function report(Throwable $exception)
  36. {
  37. parent::report($exception);
  38. }
  39. public function render( $request, Throwable $exception)
  40. {
  41. $errMsg='';
  42. try{
  43. $type = $exception->type ?? 'error';
  44. $errMsg=(function()use($request,$exception){
  45. return $errMsg=
  46. '异常: '
  47. .' URL:'.(
  48. method_exists($request,'fullUrl')
  49. ?($request->fullUrl()??'')
  50. :'')
  51. .' method:'.(
  52. method_exists($request,'method')
  53. ?($request->method()??'')
  54. :'')
  55. .' params:'.(
  56. method_exists($request,'all')
  57. ?json_encode($request->all()??'')
  58. :'')
  59. .' errors:'.(
  60. method_exists($exception,'errors')
  61. ?(json_encode($exception->errors(),JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)??'')
  62. :'')
  63. .' header:'.(
  64. method_exists($request,'header')
  65. ?json_encode($request->headers->all()??'')
  66. :'')
  67. .' code:'.(
  68. method_exists($exception,'getStatusCode')
  69. ?($exception->getStatusCode()??'')
  70. :'')
  71. .' message:'.(
  72. method_exists($exception,'getMessage')
  73. ?($exception->getMessage()??'')
  74. :'')
  75. .' trace:'.
  76. (method_exists($exception,'getTraceAsString')
  77. ?($exception->getTraceAsString()??'')
  78. :'');
  79. })();
  80. if($request->is("api/*")
  81. && $exception instanceof ValidationException){
  82. return response()->json($exception->errors(),200,[],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  83. }
  84. if(strpos($exception->getMessage(),'This action is unauthorized')!==false){
  85. return response()->view('exception.unauthorized');
  86. }
  87. if (method_exists($exception,'getStatusCode')) {
  88. $code = $exception->getStatusCode();
  89. switch ($code){
  90. case 419: $view='exception.login';break;
  91. case 404:$view='exception.404';break;
  92. default: $view='exception.default';break;
  93. }
  94. return response()->view($view,[
  95. 'code'=> $code,
  96. 'message'=>$exception->getMessage(),
  97. ]);
  98. }
  99. }catch (\Exception $e){}finally{
  100. list(
  101. $className,
  102. $functionName,
  103. $tracesAll
  104. ) =(function()use($exception){
  105. $traces=method_exists($exception,'getTraceAsString')
  106. ?($exception->getTraceAsString()??'')
  107. :'';
  108. if(!$traces)return '';
  109. preg_match('/\#0.*?\: (.*?)(-\>|\:\:)(.*?)\(/', $traces, $result);
  110. return [$result[1]??'',$result[3]??'',$traces];
  111. })();
  112. LogService::log(
  113. $className,
  114. $functionName,
  115. ($errMsg??'')
  116. .'请求:'.json_encode($request->all())
  117. .'调用堆栈r:'.$tracesAll,
  118. Auth::id()??'',
  119. $type
  120. );
  121. }
  122. return parent::render($request, $exception);
  123. }
  124. }