ExceptionHandle.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app;
  3. use app\dict\sys\AppTypeDict;
  4. use core\exception\AdminException;
  5. use core\exception\AuthException;
  6. use core\exception\ServerException;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\exception\Handle;
  11. use think\exception\HttpException;
  12. use think\exception\HttpResponseException;
  13. use think\exception\ValidateException;
  14. use think\facade\Log;
  15. use think\Response;
  16. use Throwable;
  17. use UnexpectedValueException;
  18. /**
  19. * 应用异常处理类
  20. */
  21. class ExceptionHandle extends Handle
  22. {
  23. /**
  24. * 不需要记录信息(日志)的异常类列表
  25. * @var array
  26. */
  27. protected $ignoreReport = [
  28. HttpException::class,
  29. HttpResponseException::class,
  30. ModelNotFoundException::class,
  31. DataNotFoundException::class,
  32. ValidateException::class,
  33. ];
  34. /**
  35. * 记录异常信息(包括日志或者其它方式记录)
  36. *
  37. * @access public
  38. * @param Throwable $e
  39. * @return void
  40. */
  41. public function report(Throwable $e): void
  42. {
  43. // 使用内置的方式记录异常日志
  44. // parent::report($exception);
  45. if (!$this->isIgnoreReport($e)) {
  46. $data = [
  47. 'file' => $e->getFile(),
  48. 'line' => $e->getLine(),
  49. 'message' => $e->getMessage(),
  50. 'trace' => $e->getTrace(),
  51. 'previous' => $e->getPrevious(),
  52. ];
  53. //这个类可能会分开拆成两个
  54. $app_type = request()->appType() ;
  55. $app_type = empty($app_type) ? str_replace('/', '', request()->rootUrl()) : $app_type;
  56. //写入日志内容
  57. $log = [
  58. '服务主体:'.($app_type == AppTypeDict::ADMIN ? request()->uid() : request()->memberId()),//服务发起者 //用户ID
  59. 'IP:'.request()->ip(),//ip
  60. '耗时(毫秒):'.ceil((microtime(true) * 1000) - (request()->time(true) * 1000)),//耗时(毫秒)
  61. '请求类型:'.request()->method(),//请求类型
  62. '应用:'.$app_type,//应用
  63. '路由:'.request()->baseUrl(),//路由
  64. '请求参数:'.json_encode(request()->param() ?? []),//请求参数
  65. '错误信息:'.json_encode($data),//错误信息
  66. ];
  67. Log::write('DEBUG:>>>>>>>>>'.PHP_EOL.implode(PHP_EOL, $log).PHP_EOL.'---------', 'error');
  68. }
  69. }
  70. /**
  71. * Render an exception into an HTTP response.
  72. * @access public
  73. * @param \think\Request $request
  74. * @param Throwable $e
  75. * @return Response
  76. */
  77. public function render($request, Throwable $e): Response
  78. {
  79. // 添加自定义异常处理机制
  80. $massageData = env('app_debug', false) ? [
  81. 'file' => $e->getFile(),
  82. 'line' => $e->getLine(),
  83. 'message' => $e->getMessage(),
  84. 'trace' => $e->getTrace(),
  85. 'previous' => $e->getPrevious(),
  86. ] : [];
  87. // 添加自定义异常处理机制
  88. if (strpos($e->getMessage(), 'open_basedir') !== false) {
  89. return fail('OPEN_BASEDIR_ERROR');
  90. }
  91. if ($e instanceof DbException) {
  92. return fail(get_lang('DATA_GET_FAIL').':'.$e->getMessage(), [
  93. 'file' => $e->getFile(),
  94. 'line' => $e->getLine(),
  95. 'message' => $e->getMessage(),
  96. 'trace' => $e->getTrace(),
  97. 'previous' => $e->getPrevious(),
  98. ]);
  99. } elseif ($e instanceof ValidateException) {
  100. return fail($e->getMessage());
  101. } else if($e instanceof UnexpectedValueException){
  102. return fail($e->getMessage(), [], 401);
  103. }else if($e instanceof AuthException){
  104. return fail($e->getMessage(), [], $e->getCode() ?: 400);
  105. }else if($e instanceof ServerException){
  106. return fail($e->getMessage(), http_code:$e->getCode());
  107. }else {
  108. return fail($e->getMessage(), $massageData);
  109. }
  110. }
  111. }