ExceptionHandle.php 3.2 KB

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