feat: 同步下新环境
This commit is contained in:
@@ -12,8 +12,11 @@
|
||||
namespace think\exception;
|
||||
|
||||
use Exception;
|
||||
use think\App;
|
||||
use think\Config;
|
||||
use think\console\Output;
|
||||
use think\Container;
|
||||
use think\Lang;
|
||||
use think\Log;
|
||||
use think\Response;
|
||||
|
||||
class Handle
|
||||
@@ -31,7 +34,6 @@ class Handle
|
||||
/**
|
||||
* Report or log an exception.
|
||||
*
|
||||
* @access public
|
||||
* @param \Exception $exception
|
||||
* @return void
|
||||
*/
|
||||
@@ -39,7 +41,7 @@ class Handle
|
||||
{
|
||||
if (!$this->isIgnoreReport($exception)) {
|
||||
// 收集异常数据
|
||||
if (Container::get('app')->isDebug()) {
|
||||
if (App::$debug) {
|
||||
$data = [
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine(),
|
||||
@@ -55,11 +57,11 @@ class Handle
|
||||
$log = "[{$data['code']}]{$data['message']}";
|
||||
}
|
||||
|
||||
if (Container::get('app')->config('log.record_trace')) {
|
||||
if (Config::get('record_trace')) {
|
||||
$log .= "\r\n" . $exception->getTraceAsString();
|
||||
}
|
||||
|
||||
Container::get('log')->record($log, 'error');
|
||||
Log::record($log, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,14 +72,12 @@ class Handle
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render an exception into an HTTP response.
|
||||
*
|
||||
* @access public
|
||||
* @param \Exception $e
|
||||
* @return Response
|
||||
*/
|
||||
@@ -85,7 +85,6 @@ class Handle
|
||||
{
|
||||
if ($this->render && $this->render instanceof \Closure) {
|
||||
$result = call_user_func_array($this->render, [$e]);
|
||||
|
||||
if ($result) {
|
||||
return $result;
|
||||
}
|
||||
@@ -99,30 +98,26 @@ class Handle
|
||||
}
|
||||
|
||||
/**
|
||||
* @access public
|
||||
* @param Output $output
|
||||
* @param Exception $e
|
||||
* @param Output $output
|
||||
* @param Exception $e
|
||||
*/
|
||||
public function renderForConsole(Output $output, Exception $e)
|
||||
{
|
||||
if (Container::get('app')->isDebug()) {
|
||||
if (App::$debug) {
|
||||
$output->setVerbosity(Output::VERBOSITY_DEBUG);
|
||||
}
|
||||
|
||||
$output->renderException($e);
|
||||
}
|
||||
|
||||
/**
|
||||
* @access protected
|
||||
* @param HttpException $e
|
||||
* @param HttpException $e
|
||||
* @return Response
|
||||
*/
|
||||
protected function renderHttpException(HttpException $e)
|
||||
{
|
||||
$status = $e->getStatusCode();
|
||||
$template = Container::get('app')->config('http_exception_template');
|
||||
|
||||
if (!Container::get('app')->isDebug() && !empty($template[$status])) {
|
||||
$template = Config::get('http_exception_template');
|
||||
if (!App::$debug && !empty($template[$status])) {
|
||||
return Response::create($template[$status], 'view', $status)->assign(['e' => $e]);
|
||||
} else {
|
||||
return $this->convertExceptionToResponse($e);
|
||||
@@ -130,14 +125,13 @@ class Handle
|
||||
}
|
||||
|
||||
/**
|
||||
* @access protected
|
||||
* @param Exception $exception
|
||||
* @param Exception $exception
|
||||
* @return Response
|
||||
*/
|
||||
protected function convertExceptionToResponse(Exception $exception)
|
||||
{
|
||||
// 收集异常数据
|
||||
if (Container::get('app')->isDebug()) {
|
||||
if (App::$debug) {
|
||||
// 调试模式,获取详细的错误信息
|
||||
$data = [
|
||||
'name' => get_class($exception),
|
||||
@@ -166,9 +160,9 @@ class Handle
|
||||
'message' => $this->getMessage($exception),
|
||||
];
|
||||
|
||||
if (!Container::get('app')->config('show_error_msg')) {
|
||||
if (!Config::get('show_error_msg')) {
|
||||
// 不显示详细错误信息
|
||||
$data['message'] = Container::get('app')->config('error_message');
|
||||
$data['message'] = Config::get('error_message');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,11 +175,10 @@ class Handle
|
||||
|
||||
ob_start();
|
||||
extract($data);
|
||||
include Container::get('app')->config('exception_tmpl');
|
||||
|
||||
include Config::get('exception_tmpl');
|
||||
// 获取并清空缓存
|
||||
$content = ob_get_clean();
|
||||
$response = Response::create($content, 'html');
|
||||
$response = new Response($content, 'html');
|
||||
|
||||
if ($exception instanceof HttpException) {
|
||||
$statusCode = $exception->getStatusCode();
|
||||
@@ -196,62 +189,52 @@ class Handle
|
||||
$statusCode = 500;
|
||||
}
|
||||
$response->code($statusCode);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误编码
|
||||
* ErrorException则使用错误级别作为错误编码
|
||||
* @access protected
|
||||
* @param \Exception $exception
|
||||
* @return integer 错误编码
|
||||
*/
|
||||
protected function getCode(Exception $exception)
|
||||
{
|
||||
$code = $exception->getCode();
|
||||
|
||||
if (!$code && $exception instanceof ErrorException) {
|
||||
$code = $exception->getSeverity();
|
||||
}
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误信息
|
||||
* ErrorException则使用错误级别作为错误编码
|
||||
* @access protected
|
||||
* @param \Exception $exception
|
||||
* @return string 错误信息
|
||||
*/
|
||||
protected function getMessage(Exception $exception)
|
||||
{
|
||||
$message = $exception->getMessage();
|
||||
|
||||
if (PHP_SAPI == 'cli') {
|
||||
if (IS_CLI) {
|
||||
return $message;
|
||||
}
|
||||
|
||||
$lang = Container::get('lang');
|
||||
|
||||
if (strpos($message, ':')) {
|
||||
$name = strstr($message, ':', true);
|
||||
$message = $lang->has($name) ? $lang->get($name) . strstr($message, ':') : $message;
|
||||
$message = Lang::has($name) ? Lang::get($name) . strstr($message, ':') : $message;
|
||||
} elseif (strpos($message, ',')) {
|
||||
$name = strstr($message, ',', true);
|
||||
$message = $lang->has($name) ? $lang->get($name) . ':' . substr(strstr($message, ','), 1) : $message;
|
||||
} elseif ($lang->has($message)) {
|
||||
$message = $lang->get($message);
|
||||
$message = Lang::has($name) ? Lang::get($name) . ':' . substr(strstr($message, ','), 1) : $message;
|
||||
} elseif (Lang::has($message)) {
|
||||
$message = Lang::get($message);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取出错文件内容
|
||||
* 获取错误的前9行和后9行
|
||||
* @access protected
|
||||
* @param \Exception $exception
|
||||
* @return array 错误文件内容
|
||||
*/
|
||||
@@ -270,37 +253,30 @@ class Handle
|
||||
} catch (Exception $e) {
|
||||
$source = [];
|
||||
}
|
||||
|
||||
return $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取异常扩展信息
|
||||
* 用于非调试模式html返回类型显示
|
||||
* @access protected
|
||||
* @param \Exception $exception
|
||||
* @return array 异常类定义的扩展数据
|
||||
*/
|
||||
protected function getExtendData(Exception $exception)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if ($exception instanceof \think\Exception) {
|
||||
$data = $exception->getData();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取常量列表
|
||||
* @access private
|
||||
* @return array 常量列表
|
||||
*/
|
||||
private static function getConst()
|
||||
{
|
||||
$const = get_defined_constants(true);
|
||||
|
||||
return isset($const['user']) ? $const['user'] : [];
|
||||
return get_defined_constants(true)['user'];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user