ExceptionHandle.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\exception\Handle;
  16. use app\common\exception\BaseException;
  17. /**
  18. * 应用异常处理类
  19. */
  20. class ExceptionHandle extends Handle
  21. {
  22. // 状态码
  23. private $status;
  24. // 错误信息
  25. private $message;
  26. // 附加数据
  27. public $data = [];
  28. /**
  29. * 记录异常信息(包括日志或者其它方式记录)
  30. * @access public
  31. * @param Throwable $exception
  32. * @return void
  33. */
  34. public function report(Throwable $exception): void
  35. {
  36. // 不使用内置的方式记录异常日志
  37. // parent::report($exception);
  38. }
  39. /**
  40. * Render an exception into an HTTP response.
  41. *
  42. * @access public
  43. * @param $request
  44. * @param Throwable $e
  45. * @return Response
  46. */
  47. public function render($request, Throwable $e): Response
  48. {
  49. if ($e instanceof BaseException) {
  50. $this->status = $e->status;
  51. $this->message = $e->message;
  52. $this->data = $e->data;
  53. return $this->renderJson();
  54. }
  55. $this->status = config('status.error');
  56. $this->message = $e->getMessage() ?: '很抱歉,服务器内部错误';
  57. // 如果是debug模式, 输出调试信息
  58. if (is_debug()) {
  59. return $this->renderDebug($e);
  60. }
  61. // 将异常写入日志
  62. $this->recordErrorLog($e);
  63. return $this->renderJson();
  64. }
  65. /**
  66. * 返回json格式数据
  67. * @param array $extend 扩展的数据
  68. * @return \think\response\Json
  69. */
  70. private function renderJson($extend = [])
  71. {
  72. $jsonData = ['message' => $this->message, 'status' => $this->status, 'data' => $this->data];
  73. return json(array_merge($jsonData, $extend));
  74. }
  75. /**
  76. * 返回json格式数据 (debug模式)
  77. * @param Throwable $e
  78. * @return \think\response\Json
  79. */
  80. private function renderDebug(Throwable $e)
  81. {
  82. $debug = [
  83. 'name' => get_class($e),
  84. 'file' => $e->getFile(),
  85. 'line' => $e->getLine(),
  86. 'code' => $this->getCode($e),
  87. 'message' => $this->getMessage($e),
  88. 'trace' => $e->getTrace(),
  89. 'source' => $this->getSourceCode($e),
  90. ];
  91. return $this->renderJson(['debug' => $debug]);
  92. }
  93. /**
  94. * 将异常写入日志
  95. * @param Throwable $e
  96. */
  97. private function recordErrorLog(Throwable $e)
  98. {
  99. // 生成日志内容
  100. $data = [
  101. 'file' => $e->getFile(),
  102. 'line' => $e->getLine(),
  103. 'message' => $this->getMessage($e),
  104. 'status' => $this->getCode($e),
  105. ];
  106. $log = "[{$data['status']}]{$data['message']} [{$data['file']}:{$data['line']}]";
  107. $log .= "\r\n" . $e->getTraceAsString();
  108. // 写入日志文件
  109. Log::record($log, 'error');
  110. }
  111. }