ExceptionHandle.php 4.7 KB

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