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\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. dd(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
  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. }