Handler.php 4.4 KB

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