mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
完善框架的异常处理
This commit is contained in:
@@ -11,9 +11,9 @@
|
||||
|
||||
namespace think;
|
||||
|
||||
use think\Config;
|
||||
use think\exception\ErrorException;
|
||||
use think\Log;
|
||||
use think\exception\Handle;
|
||||
use think\exception\ThrowableError;
|
||||
|
||||
class Error
|
||||
{
|
||||
@@ -23,66 +23,29 @@ class Error
|
||||
*/
|
||||
public static function register()
|
||||
{
|
||||
error_reporting(-1);
|
||||
set_error_handler([__CLASS__, 'appError']);
|
||||
set_exception_handler([__CLASS__, 'appException']);
|
||||
register_shutdown_function([__CLASS__, 'appShutdown']);
|
||||
|
||||
if (!APP_DEBUG) {
|
||||
ini_set('display_errors', 'Off');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception Handler
|
||||
* @param \Exception $exception
|
||||
* @return bool true-禁止往下传播已处理过的异常
|
||||
* @param \Exception $e
|
||||
*/
|
||||
public static function appException($exception)
|
||||
public static function appException($e)
|
||||
{
|
||||
// 收集异常数据
|
||||
if (APP_DEBUG) {
|
||||
// 调试模式,获取详细的错误信息
|
||||
$data = [
|
||||
'name' => get_class($exception),
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine(),
|
||||
'message' => $exception->getMessage(),
|
||||
'trace' => $exception->getTrace(),
|
||||
'code' => self::getCode($exception),
|
||||
'source' => self::getSourceCode($exception),
|
||||
'datas' => self::getExtendData($exception),
|
||||
|
||||
'tables' => [
|
||||
'GET Data' => $_GET,
|
||||
'POST Data' => $_POST,
|
||||
'Files' => $_FILES,
|
||||
'Cookies' => $_COOKIE,
|
||||
'Session' => isset($_SESSION) ? $_SESSION : [],
|
||||
'Server/Request Data' => $_SERVER,
|
||||
'Environment Variables' => $_ENV,
|
||||
'ThinkPHP Constants' => self::getTPConst(),
|
||||
],
|
||||
];
|
||||
$log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
|
||||
} else {
|
||||
// 部署模式仅显示 Code 和 Message
|
||||
$data = [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => $exception->getMessage(),
|
||||
];
|
||||
$log = "[{$data['code']}]{$data['message']}";
|
||||
if (!$e instanceof \Exception) {
|
||||
$e = new ThrowableError($e);
|
||||
}
|
||||
|
||||
self::getExceptionHandler()->report($e);
|
||||
|
||||
// 记录异常日志
|
||||
Log::record($log, 'error');
|
||||
|
||||
/* 非API模式下的部署模式,跳转到指定的 Error Page */
|
||||
$error_page = Config::get('error_page');
|
||||
if (!(APP_DEBUG || IS_API) && !empty($error_page)) {
|
||||
header("Location: {$error_page}");
|
||||
} else {
|
||||
// 输出错误信息
|
||||
self::output($exception, $data);
|
||||
}
|
||||
|
||||
// 禁止往下传播已处理过的异常
|
||||
return true;
|
||||
self::getExceptionHandler()->render($e)->send();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,31 +54,24 @@ class Error
|
||||
* @param integer $errstr 详细错误信息
|
||||
* @param string $errfile 出错的文件
|
||||
* @param integer $errline 出错行号
|
||||
* @return bool true-禁止往下传播已处理过的异常
|
||||
* @param array $errcontext
|
||||
* @return bool true-禁止往下传播已处理过的异常
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public static function appError($errno, $errstr, $errfile = null, $errline = 0, array $errcontext = [])
|
||||
public static function appError($errno, $errstr, $errfile = '', $errline = 0, $errcontext = [])
|
||||
{
|
||||
if ($errno & Config::get('exception_ignore_type')) {
|
||||
// 忽略的异常记录到日志
|
||||
Log::record("[{$errno}]{$errstr}[{$errfile}:{$errline}]", 'notice');
|
||||
} else {
|
||||
if (error_reporting() & $errno) {
|
||||
// 将错误信息托管至 think\exception\ErrorException
|
||||
throw new ErrorException($errno, $errstr, $errfile, $errline, $errcontext);
|
||||
// 禁止往下传播已处理过的异常
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown Handler
|
||||
* @return bool true-禁止往下传播已处理过的异常; false-未处理的异常继续传播
|
||||
*/
|
||||
public static function appShutdown()
|
||||
{
|
||||
// 写入日志
|
||||
Log::save();
|
||||
|
||||
if ($error = error_get_last()) {
|
||||
if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {
|
||||
// 将错误信息托管至think\ErrorException
|
||||
$exception = new ErrorException(
|
||||
$error['type'],
|
||||
@@ -124,108 +80,46 @@ class Error
|
||||
$error['line']
|
||||
);
|
||||
|
||||
/**
|
||||
* Shutdown handler 中的异常将不被往下传播
|
||||
* 所以,这里我们必须手动传播而不能像 Error handler 中那样 throw
|
||||
*/
|
||||
self::appException($exception);
|
||||
// 禁止往下传播已处理过的异常
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
// 写入日志
|
||||
Log::save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出异常信息
|
||||
* @param \Exception $exception
|
||||
* @param array $data 异常信息
|
||||
* @return void
|
||||
* 确定错误类型是否致命
|
||||
*
|
||||
* @param int $type
|
||||
* @return bool
|
||||
*/
|
||||
public static function output($exception, array $data)
|
||||
protected static function isFatal($type)
|
||||
{
|
||||
http_response_code($exception instanceof Exception ? $exception->getHttpStatus() : 500);
|
||||
|
||||
$type = Config::get('default_return_type');
|
||||
if (!APP_DEBUG && !Config::get('show_error_msg')) {
|
||||
// 不显示详细错误信息
|
||||
$data['message'] = Config::get('error_message');
|
||||
}
|
||||
if (IS_API && 'html' != $type) {
|
||||
// 异常信息输出监听
|
||||
APP_HOOK && Hook::listen('error_output', $data);
|
||||
// 输出异常内容
|
||||
Response::instance()->send($data, $type, Config::get('response_return'));
|
||||
} else {
|
||||
//ob_end_clean();
|
||||
extract($data);
|
||||
include Config::get('exception_tmpl');
|
||||
}
|
||||
return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误编码
|
||||
* ErrorException则使用错误级别作为错误编码
|
||||
* @param \Exception $exception
|
||||
* @return integer 错误编码
|
||||
*/
|
||||
private static function getCode($exception)
|
||||
{
|
||||
$code = $exception->getCode();
|
||||
if (!$code && $exception instanceof ErrorException) {
|
||||
$code = $exception->getSeverity();
|
||||
}
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取出错文件内容
|
||||
* 获取错误的前9行和后9行
|
||||
* @param \Exception $exception
|
||||
* @return array 错误文件内容
|
||||
* Get an instance of the exception handler.
|
||||
*
|
||||
* @return \think\exception\Handle
|
||||
*/
|
||||
private static function getSourceCode($exception)
|
||||
protected static function getExceptionHandler()
|
||||
{
|
||||
// 读取前9行和后9行
|
||||
$line = $exception->getLine();
|
||||
$first = ($line - 9 > 0) ? $line - 9 : 1;
|
||||
static $handle;
|
||||
|
||||
try {
|
||||
$contents = file($exception->getFile());
|
||||
$source = [
|
||||
'first' => $first,
|
||||
'source' => array_slice($contents, $first - 1, 19),
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
$source = [];
|
||||
}
|
||||
return $source;
|
||||
}
|
||||
if (!$handle) {
|
||||
|
||||
/**
|
||||
* 获取异常扩展信息
|
||||
* 用于非调试模式html返回类型显示
|
||||
* @param \Exception $exception
|
||||
* @return array 异常类定义的扩展数据
|
||||
*/
|
||||
private static function getExtendData($exception)
|
||||
{
|
||||
$data = [];
|
||||
if ($exception instanceof Exception) {
|
||||
$data = $exception->getData();
|
||||
if ($class = Config::get('exception_handle')) {
|
||||
if (class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) {
|
||||
$handle = new $class;
|
||||
}
|
||||
}
|
||||
if (!$handle) {
|
||||
$handle = new Handle();
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ThinkPHP常量列表
|
||||
* @return array 常量列表
|
||||
*/
|
||||
private static function getTPConst()
|
||||
{
|
||||
$consts = ['THINK_VERSION', 'THINK_PATH', 'LIB_PATH', 'EXTEND_PATH', 'MODE_PATH', 'CORE_PATH', 'TRAIT_PATH', 'APP_PATH', 'RUNTIME_PATH', 'LOG_PATH', 'CACHE_PATH', 'TEMP_PATH', 'MODULE_PATH', 'VIEW_PATH', 'APP_NAMESPACE', 'COMMON_MODULE', 'APP_MULTI_MODULE', 'MODULE_ALIAS', 'MODULE_NAME', 'CONTROLLER_NAME', 'ACTION_NAME', 'MODEL_LAYER', 'VIEW_LAYER', 'CONTROLLER_LAYER', 'APP_DEBUG', 'APP_HOOK', 'ENV_PREFIX', 'IS_API', 'VENDOR_PATH', 'APP_AUTO_RUN', 'APP_MODE', 'REQUEST_METHOD', 'IS_CGI', 'IS_WIN', 'IS_API', 'IS_CLI', 'IS_GET', 'IS_POST', 'IS_PUT', 'IS_AJAX', 'IS_DELETE', 'NOW_TIME', 'LANG_SET', 'EXT', 'DS', '__INFO__', '__EXT__'];
|
||||
foreach ($consts as $const) {
|
||||
$data[$const] = defined($const) ? constant($const) : 'undefined';
|
||||
}
|
||||
return $data;
|
||||
return $handle;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user