Handle.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\exception;
  12. use Exception;
  13. use think\App;
  14. use think\Config;
  15. use think\console\Output;
  16. use think\Lang;
  17. use think\Log;
  18. use think\Response;
  19. class Handle
  20. {
  21. protected $ignoreReport = [
  22. '\\think\\exception\\HttpException',
  23. ];
  24. /**
  25. * Report or log an exception.
  26. *
  27. * @param \Exception $exception
  28. * @return void
  29. */
  30. public function report(Exception $exception)
  31. {
  32. if (!$this->isIgnoreReport($exception)) {
  33. // 收集异常数据
  34. if (App::$debug) {
  35. $data = [
  36. 'file' => $exception->getFile(),
  37. 'line' => $exception->getLine(),
  38. 'message' => $this->getMessage($exception),
  39. 'code' => $this->getCode($exception),
  40. ];
  41. $log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
  42. } else {
  43. $data = [
  44. 'code' => $this->getCode($exception),
  45. 'message' => $this->getMessage($exception),
  46. ];
  47. $log = "[{$data['code']}]{$data['message']}";
  48. }
  49. Log::record($log, 'error');
  50. }
  51. }
  52. protected function isIgnoreReport(Exception $exception)
  53. {
  54. foreach ($this->ignoreReport as $class) {
  55. if ($exception instanceof $class) {
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. /**
  62. * Render an exception into an HTTP response.
  63. *
  64. * @param \Exception $e
  65. * @return Response
  66. */
  67. public function render(Exception $e)
  68. {
  69. if ($e instanceof HttpException) {
  70. return $this->renderHttpException($e);
  71. } else {
  72. return $this->convertExceptionToResponse($e);
  73. }
  74. }
  75. /**
  76. * @param Output $output
  77. * @param Exception $e
  78. */
  79. public function renderForConsole(Output $output, Exception $e)
  80. {
  81. if (App::$debug) {
  82. $output->setVerbosity(Output::VERBOSITY_DEBUG);
  83. }
  84. $output->renderException($e);
  85. }
  86. /**
  87. * @param HttpException $e
  88. * @return Response
  89. */
  90. protected function renderHttpException(HttpException $e)
  91. {
  92. $status = $e->getStatusCode();
  93. $template = Config::get('http_exception_template');
  94. if (!App::$debug && !empty($template[$status])) {
  95. return Response::create($template[$status], 'view', $status)->assign(['e' => $e]);
  96. } else {
  97. return $this->convertExceptionToResponse($e);
  98. }
  99. }
  100. /**
  101. * @param Exception $exception
  102. * @return Response
  103. */
  104. protected function convertExceptionToResponse(Exception $exception)
  105. {
  106. // 收集异常数据
  107. if (App::$debug) {
  108. // 调试模式,获取详细的错误信息
  109. $data = [
  110. 'name' => get_class($exception),
  111. 'file' => $exception->getFile(),
  112. 'line' => $exception->getLine(),
  113. 'message' => $this->getMessage($exception),
  114. 'trace' => $exception->getTrace(),
  115. 'code' => $this->getCode($exception),
  116. 'source' => $this->getSourceCode($exception),
  117. 'datas' => $this->getExtendData($exception),
  118. 'tables' => [
  119. 'GET Data' => $_GET,
  120. 'POST Data' => $_POST,
  121. 'Files' => $_FILES,
  122. 'Cookies' => $_COOKIE,
  123. 'Session' => isset($_SESSION) ? $_SESSION : [],
  124. 'Server/Request Data' => $_SERVER,
  125. 'Environment Variables' => $_ENV,
  126. 'ThinkPHP Constants' => $this->getConst(),
  127. ],
  128. ];
  129. } else {
  130. // 部署模式仅显示 Code 和 Message
  131. $data = [
  132. 'code' => $this->getCode($exception),
  133. 'message' => $this->getMessage($exception),
  134. ];
  135. if (!Config::get('show_error_msg')) {
  136. // 不显示详细错误信息
  137. $data['message'] = Config::get('error_message');
  138. }
  139. }
  140. //保留一层
  141. while (ob_get_level() > 1) {
  142. ob_end_clean();
  143. }
  144. $data['echo'] = ob_get_clean();
  145. ob_start();
  146. extract($data);
  147. include Config::get('exception_tmpl');
  148. // 获取并清空缓存
  149. $content = ob_get_clean();
  150. $response = new Response($content, 'html');
  151. if ($exception instanceof HttpException) {
  152. $statusCode = $exception->getStatusCode();
  153. $response->header($exception->getHeaders());
  154. }
  155. if (!isset($statusCode)) {
  156. $statusCode = 500;
  157. }
  158. $response->code($statusCode);
  159. return $response;
  160. }
  161. /**
  162. * 获取错误编码
  163. * ErrorException则使用错误级别作为错误编码
  164. * @param \Exception $exception
  165. * @return integer 错误编码
  166. */
  167. protected function getCode(Exception $exception)
  168. {
  169. $code = $exception->getCode();
  170. if (!$code && $exception instanceof ErrorException) {
  171. $code = $exception->getSeverity();
  172. }
  173. return $code;
  174. }
  175. /**
  176. * 获取错误信息
  177. * ErrorException则使用错误级别作为错误编码
  178. * @param \Exception $exception
  179. * @return string 错误信息
  180. */
  181. protected function getMessage(Exception $exception)
  182. {
  183. $message = $exception->getMessage();
  184. if (IS_CLI) {
  185. return $message;
  186. }
  187. if (strpos($message, ':')) {
  188. $name = strstr($message, ':', true);
  189. $message = Lang::has($name) ? Lang::get($name) . strstr($message, ':') : $message;
  190. } elseif (strpos($message, ',')) {
  191. $name = strstr($message, ',', true);
  192. $message = Lang::has($name) ? Lang::get($name) . ':' . substr(strstr($message, ','), 1) : $message;
  193. } elseif (Lang::has($message)) {
  194. $message = Lang::get($message);
  195. }
  196. return $message;
  197. }
  198. /**
  199. * 获取出错文件内容
  200. * 获取错误的前9行和后9行
  201. * @param \Exception $exception
  202. * @return array 错误文件内容
  203. */
  204. protected function getSourceCode(Exception $exception)
  205. {
  206. // 读取前9行和后9行
  207. $line = $exception->getLine();
  208. $first = ($line - 9 > 0) ? $line - 9 : 1;
  209. try {
  210. $contents = file($exception->getFile());
  211. $source = [
  212. 'first' => $first,
  213. 'source' => array_slice($contents, $first - 1, 19),
  214. ];
  215. } catch (Exception $e) {
  216. $source = [];
  217. }
  218. return $source;
  219. }
  220. /**
  221. * 获取异常扩展信息
  222. * 用于非调试模式html返回类型显示
  223. * @param \Exception $exception
  224. * @return array 异常类定义的扩展数据
  225. */
  226. protected function getExtendData(Exception $exception)
  227. {
  228. $data = [];
  229. if ($exception instanceof \think\Exception) {
  230. $data = $exception->getData();
  231. }
  232. return $data;
  233. }
  234. /**
  235. * 获取常量列表
  236. * @return array 常量列表
  237. */
  238. private static function getConst()
  239. {
  240. return get_defined_constants(true)['user'];
  241. }
  242. }