ExceptionHandle.php 4.8 KB

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