ExceptionHandle.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace cores;
  13. use Throwable;
  14. use think\Response;
  15. use think\response\Json;
  16. use think\facade\Log;
  17. use think\facade\Request;
  18. use think\exception\Handle;
  19. use think\db\exception\PDOException;
  20. use think\exception\HttpResponseException;
  21. use cores\exception\BaseException;
  22. /**
  23. * 应用异常处理类
  24. */
  25. class ExceptionHandle extends Handle
  26. {
  27. // 状态码
  28. private int $status = 200;
  29. // 错误信息
  30. private string $message;
  31. // 附加数据
  32. public array $data = [];
  33. /**
  34. * 记录异常信息(包括日志或者其它方式记录)
  35. * @access public
  36. * @param Throwable $exception
  37. * @return void
  38. */
  39. public function report(Throwable $exception): void
  40. {
  41. // 不使用内置的方式记录异常日志
  42. // parent::report($exception);
  43. }
  44. /**
  45. * Render an exception into an HTTP response.
  46. *
  47. * @access public
  48. * @param $request
  49. * @param Throwable $e
  50. * @return Response
  51. */
  52. public function render($request, Throwable $e): Response
  53. {
  54. if ($e instanceof HttpResponseException) {
  55. return $e->getResponse();
  56. }
  57. // 手动触发的异常 BaseException
  58. if ($e instanceof BaseException) {
  59. $this->status = $e->status;
  60. $this->message = $e->message;
  61. $this->data = $e->data;
  62. $extend = property_exists($e, 'extend') ? $e->extend : [];
  63. return $this->output($extend);
  64. }
  65. // 系统运行的异常
  66. $this->status = config('status.error');
  67. $this->message = $e->getMessage() ?: '很抱歉,服务器内部错误';
  68. // 如果是debug模式, 输出调试信息
  69. if (is_debug()) {
  70. return $this->outputDebug($e);
  71. }
  72. // 将运行异常写入日志
  73. $this->errorLog($e);
  74. return $this->output();
  75. }
  76. /**
  77. * 返回json格式数据
  78. * @param array $extend 扩展的数据
  79. * @return Json
  80. */
  81. private function output(array $extend = []): Json
  82. {
  83. $jsonData = ['message' => $this->message, 'status' => $this->status, 'data' => $this->data];
  84. return json(array_merge($jsonData, $extend));
  85. }
  86. /**
  87. * 返回json格式数据 (debug模式)
  88. * @param Throwable $e
  89. * @return Json
  90. */
  91. private function outputDebug(Throwable $e): Json
  92. {
  93. $debug = [
  94. 'name' => get_class($e),
  95. 'file' => $e->getFile(),
  96. 'line' => $e->getLine(),
  97. 'code' => $this->getCode($e),
  98. 'message' => $this->getMessage($e),
  99. 'trace' => $e->getTrace(),
  100. 'source' => $this->getSourceCode($e),
  101. ];
  102. return $this->output(['debug' => $debug]);
  103. }
  104. /**
  105. * 将异常写入日志
  106. * @param Throwable $e
  107. */
  108. private function errorLog(Throwable $e)
  109. {
  110. // 错误信息
  111. $data = [
  112. 'file' => $e->getFile(),
  113. 'line' => $e->getLine(),
  114. 'message' => $this->getMessage($e),
  115. 'status' => $this->getCode($e),
  116. ];
  117. // 日志内容
  118. $log = $this->getVisitor();
  119. $log .= "\r\n" . "[ message ] [{$data['status']}] {$data['message']}";
  120. $log .= "\r\n" . "[ file ] {$data['file']}:{$data['line']}";
  121. // $log .= "\r\n" . "[ time ] " . format_time(time());
  122. $log .= "\r\n" . '[ header ] ' . print_r(Request::header(), true);
  123. $log .= '[ param ] ' . print_r(Request::param(), true);
  124. // 如果是数据库报错, 则记录sql语句
  125. if ($e instanceof PDOException) {
  126. $log .= "[ Error SQL ] " . $e->getData()['Database Status']['Error SQL'];
  127. $log .= "\r\n";
  128. }
  129. $log .= "\r\n" . $e->getTraceAsString();
  130. $log .= "\r\n" . '--------------------------------------------------------------------------------------------';
  131. // 写入日志文件
  132. Log::record($log, 'error');
  133. }
  134. /**
  135. * 获取请求路径信息
  136. * @return string
  137. */
  138. private function getVisitor(): string
  139. {
  140. $data = [Request::ip(), Request::method(), Request::url(true)];
  141. return implode(' ', $data);
  142. }
  143. }