Handler.php 4.2 KB

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