Handler.php 3.7 KB

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