ExceptionHandle.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\response\Json;
  15. use think\facade\Log;
  16. use think\facade\Request;
  17. use think\exception\Handle;
  18. use think\db\exception\PDOException;
  19. use think\exception\HttpResponseException;
  20. use cores\exception\BaseException;
  21. /**
  22. * 应用异常处理类
  23. */
  24. class ExceptionHandle extends Handle
  25. {
  26. // 状态码
  27. private $status;
  28. // 错误信息
  29. private $message;
  30. // 附加数据
  31. public $data = [];
  32. /**
  33. * 记录异常信息(包括日志或者其它方式记录)
  34. * @access public
  35. * @param Throwable $exception
  36. * @return void
  37. */
  38. public function report(Throwable $exception): void
  39. {
  40. // 不使用内置的方式记录异常日志
  41. // parent::report($exception);
  42. }
  43. /**
  44. * Render an exception into an HTTP response.
  45. *
  46. * @access public
  47. * @param $request
  48. * @param Throwable $e
  49. * @return Response
  50. */
  51. public function render($request, Throwable $e): Response
  52. {
  53. if ($e instanceof HttpResponseException) {
  54. return $e->getResponse();
  55. }
  56. // 手动触发的异常 BaseException
  57. if ($e instanceof BaseException) {
  58. $this->status = $e->status;
  59. $this->message = $e->message;
  60. $this->data = $e->data;
  61. $extend = property_exists($e, 'extend') ? $e->extend : [];
  62. return $this->output($extend);
  63. }
  64. // 系统运行的异常
  65. $this->status = config('status.error');
  66. $this->message = $e->getMessage() ?: '很抱歉,服务器内部错误';
  67. // 如果是debug模式, 输出调试信息
  68. if (is_debug()) {
  69. return $this->outputDebug($e);
  70. }
  71. // 将运行异常写入日志
  72. $this->errorLog($e);
  73. return $this->output();
  74. }
  75. /**
  76. * 返回json格式数据
  77. * @param array $extend 扩展的数据
  78. * @return Json
  79. */
  80. private function output(array $extend = []): Json
  81. {
  82. $jsonData = ['message' => $this->message, 'status' => $this->status, 'data' => $this->data];
  83. return json(array_merge($jsonData, $extend));
  84. }
  85. /**
  86. * 返回json格式数据 (debug模式)
  87. * @param Throwable $e
  88. * @return Json
  89. */
  90. private function outputDebug(Throwable $e): Json
  91. {
  92. $debug = [
  93. 'name' => get_class($e),
  94. 'file' => $e->getFile(),
  95. 'line' => $e->getLine(),
  96. 'code' => $this->getCode($e),
  97. 'message' => $this->getMessage($e),
  98. 'trace' => $e->getTrace(),
  99. 'source' => $this->getSourceCode($e),
  100. ];
  101. return $this->output(['debug' => $debug]);
  102. }
  103. /**
  104. * 将异常写入日志
  105. * @param Throwable $e
  106. */
  107. private function errorLog(Throwable $e)
  108. {
  109. // 错误信息
  110. $data = [
  111. 'file' => $e->getFile(),
  112. 'line' => $e->getLine(),
  113. 'message' => $this->getMessage($e),
  114. 'status' => $this->getCode($e),
  115. ];
  116. // 日志内容
  117. $log = $this->getVisitor();
  118. $log .= "\r\n" . "[ message ] [{$data['status']}] {$data['message']}";
  119. $log .= "\r\n" . "[ file ] {$data['file']}:{$data['line']}";
  120. // $log .= "\r\n" . "[ time ] " . format_time(time());
  121. $log .= "\r\n" . '[ header ] ' . print_r(Request::header(), true);
  122. $log .= "" . '[ param ] ' . print_r(Request::param(), true);
  123. // 如果是数据库报错, 则记录sql语句
  124. if ($e instanceof PDOException) {
  125. $log .= "[ Error SQL ] " . $e->getData()['Database Status']['Error SQL'];
  126. $log .= "\r\n";
  127. }
  128. $log .= "\r\n" . $e->getTraceAsString();
  129. $log .= "\r\n" . '--------------------------------------------------------------------------------------------';
  130. // 写入日志文件
  131. Log::record($log, 'error');
  132. }
  133. /**
  134. * 获取请求路径信息
  135. * @return string
  136. */
  137. private function getVisitor(): string
  138. {
  139. $data = [Request::ip(), Request::method(), Request::url(true)];
  140. return implode(' ', $data);
  141. }
  142. }