Handler.php 3.3 KB

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