mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
完善框架的异常处理
This commit is contained in:
@@ -123,9 +123,7 @@ return [
|
|||||||
|
|
||||||
// 异常页面的模板文件
|
// 异常页面的模板文件
|
||||||
'exception_tmpl' => THINK_PATH . 'tpl' . DS . 'think_exception.tpl',
|
'exception_tmpl' => THINK_PATH . 'tpl' . DS . 'think_exception.tpl',
|
||||||
// 异常处理忽略的错误类型,支持PHP所有的错误级别常量,多个级别可以用|运算法
|
|
||||||
// 参考:http://php.net/manual/en/errorfunc.constants.php
|
|
||||||
'exception_ignore_type' => 0,
|
|
||||||
// 错误显示信息,非调试模式有效
|
// 错误显示信息,非调试模式有效
|
||||||
'error_message' => '页面错误!请稍后再试~',
|
'error_message' => '页面错误!请稍后再试~',
|
||||||
// 错误定向页面
|
// 错误定向页面
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace think;
|
namespace think;
|
||||||
|
|
||||||
|
use think\exception\HttpResponseException;
|
||||||
use think\Response;
|
use think\Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,7 +25,8 @@ class App
|
|||||||
* 执行应用程序
|
* 执行应用程序
|
||||||
* @access public
|
* @access public
|
||||||
* @param \think\Request $request Request对象
|
* @param \think\Request $request Request对象
|
||||||
* @return void
|
* @return \think\Response
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function run($request)
|
public static function run($request)
|
||||||
{
|
{
|
||||||
@@ -76,6 +78,7 @@ class App
|
|||||||
APP_DEBUG && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info');
|
APP_DEBUG && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info');
|
||||||
// 监听app_begin
|
// 监听app_begin
|
||||||
APP_HOOK && Hook::listen('app_begin', $dispatch);
|
APP_HOOK && Hook::listen('app_begin', $dispatch);
|
||||||
|
try {
|
||||||
switch ($dispatch['type']) {
|
switch ($dispatch['type']) {
|
||||||
case 'redirect':
|
case 'redirect':
|
||||||
// 执行重定向跳转
|
// 执行重定向跳转
|
||||||
@@ -103,6 +106,9 @@ class App
|
|||||||
default:
|
default:
|
||||||
throw new Exception('dispatch type not support', 10008);
|
throw new Exception('dispatch type not support', 10008);
|
||||||
}
|
}
|
||||||
|
} catch (HttpResponseException $exception){
|
||||||
|
$data = $exception->getResponse();
|
||||||
|
}
|
||||||
// 输出数据到客户端
|
// 输出数据到客户端
|
||||||
if (isset($data)) {
|
if (isset($data)) {
|
||||||
if ($data instanceof Response) {
|
if ($data instanceof Response) {
|
||||||
|
|||||||
@@ -11,9 +11,9 @@
|
|||||||
|
|
||||||
namespace think;
|
namespace think;
|
||||||
|
|
||||||
use think\Config;
|
|
||||||
use think\exception\ErrorException;
|
use think\exception\ErrorException;
|
||||||
use think\Log;
|
use think\exception\Handle;
|
||||||
|
use think\exception\ThrowableError;
|
||||||
|
|
||||||
class Error
|
class Error
|
||||||
{
|
{
|
||||||
@@ -23,66 +23,29 @@ class Error
|
|||||||
*/
|
*/
|
||||||
public static function register()
|
public static function register()
|
||||||
{
|
{
|
||||||
|
error_reporting(-1);
|
||||||
set_error_handler([__CLASS__, 'appError']);
|
set_error_handler([__CLASS__, 'appError']);
|
||||||
set_exception_handler([__CLASS__, 'appException']);
|
set_exception_handler([__CLASS__, 'appException']);
|
||||||
register_shutdown_function([__CLASS__, 'appShutdown']);
|
register_shutdown_function([__CLASS__, 'appShutdown']);
|
||||||
|
|
||||||
|
if (!APP_DEBUG) {
|
||||||
|
ini_set('display_errors', 'Off');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exception Handler
|
* Exception Handler
|
||||||
* @param \Exception $exception
|
* @param \Exception $e
|
||||||
* @return bool true-禁止往下传播已处理过的异常
|
|
||||||
*/
|
*/
|
||||||
public static function appException($exception)
|
public static function appException($e)
|
||||||
{
|
{
|
||||||
// 收集异常数据
|
if (!$e instanceof \Exception) {
|
||||||
if (APP_DEBUG) {
|
$e = new ThrowableError($e);
|
||||||
// 调试模式,获取详细的错误信息
|
|
||||||
$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']}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 记录异常日志
|
self::getExceptionHandler()->report($e);
|
||||||
Log::record($log, 'error');
|
|
||||||
|
|
||||||
/* 非API模式下的部署模式,跳转到指定的 Error Page */
|
self::getExceptionHandler()->render($e)->send();
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,31 +54,24 @@ class Error
|
|||||||
* @param integer $errstr 详细错误信息
|
* @param integer $errstr 详细错误信息
|
||||||
* @param string $errfile 出错的文件
|
* @param string $errfile 出错的文件
|
||||||
* @param integer $errline 出错行号
|
* @param integer $errline 出错行号
|
||||||
|
* @param array $errcontext
|
||||||
* @return bool true-禁止往下传播已处理过的异常
|
* @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')) {
|
if (error_reporting() & $errno) {
|
||||||
// 忽略的异常记录到日志
|
|
||||||
Log::record("[{$errno}]{$errstr}[{$errfile}:{$errline}]", 'notice');
|
|
||||||
} else {
|
|
||||||
// 将错误信息托管至 think\exception\ErrorException
|
// 将错误信息托管至 think\exception\ErrorException
|
||||||
throw new ErrorException($errno, $errstr, $errfile, $errline, $errcontext);
|
throw new ErrorException($errno, $errstr, $errfile, $errline, $errcontext);
|
||||||
// 禁止往下传播已处理过的异常
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shutdown Handler
|
* Shutdown Handler
|
||||||
* @return bool true-禁止往下传播已处理过的异常; false-未处理的异常继续传播
|
|
||||||
*/
|
*/
|
||||||
public static function appShutdown()
|
public static function appShutdown()
|
||||||
{
|
{
|
||||||
// 写入日志
|
if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {
|
||||||
Log::save();
|
|
||||||
|
|
||||||
if ($error = error_get_last()) {
|
|
||||||
// 将错误信息托管至think\ErrorException
|
// 将错误信息托管至think\ErrorException
|
||||||
$exception = new ErrorException(
|
$exception = new ErrorException(
|
||||||
$error['type'],
|
$error['type'],
|
||||||
@@ -124,108 +80,46 @@ class Error
|
|||||||
$error['line']
|
$error['line']
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
|
||||||
* Shutdown handler 中的异常将不被往下传播
|
|
||||||
* 所以,这里我们必须手动传播而不能像 Error handler 中那样 throw
|
|
||||||
*/
|
|
||||||
self::appException($exception);
|
self::appException($exception);
|
||||||
// 禁止往下传播已处理过的异常
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
// 写入日志
|
||||||
|
Log::save();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 输出异常信息
|
* 确定错误类型是否致命
|
||||||
* @param \Exception $exception
|
*
|
||||||
* @param array $data 异常信息
|
* @param int $type
|
||||||
* @return void
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function output($exception, array $data)
|
protected static function isFatal($type)
|
||||||
{
|
{
|
||||||
http_response_code($exception instanceof Exception ? $exception->getHttpStatus() : 500);
|
return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
|
||||||
|
}
|
||||||
|
|
||||||
$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');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取错误编码
|
* Get an instance of the exception handler.
|
||||||
* ErrorException则使用错误级别作为错误编码
|
*
|
||||||
* @param \Exception $exception
|
* @return \think\exception\Handle
|
||||||
* @return integer 错误编码
|
|
||||||
*/
|
*/
|
||||||
private static function getCode($exception)
|
protected static function getExceptionHandler()
|
||||||
{
|
{
|
||||||
$code = $exception->getCode();
|
static $handle;
|
||||||
if (!$code && $exception instanceof ErrorException) {
|
|
||||||
$code = $exception->getSeverity();
|
if (!$handle) {
|
||||||
|
|
||||||
|
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 $code;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return $handle;
|
||||||
* 获取出错文件内容
|
|
||||||
* 获取错误的前9行和后9行
|
|
||||||
* @param \Exception $exception
|
|
||||||
* @return array 错误文件内容
|
|
||||||
*/
|
|
||||||
private static function getSourceCode($exception)
|
|
||||||
{
|
|
||||||
// 读取前9行和后9行
|
|
||||||
$line = $exception->getLine();
|
|
||||||
$first = ($line - 9 > 0) ? $line - 9 : 1;
|
|
||||||
|
|
||||||
try {
|
|
||||||
$contents = file($exception->getFile());
|
|
||||||
$source = [
|
|
||||||
'first' => $first,
|
|
||||||
'source' => array_slice($contents, $first - 1, 19),
|
|
||||||
];
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$source = [];
|
|
||||||
}
|
|
||||||
return $source;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取异常扩展信息
|
|
||||||
* 用于非调试模式html返回类型显示
|
|
||||||
* @param \Exception $exception
|
|
||||||
* @return array 异常类定义的扩展数据
|
|
||||||
*/
|
|
||||||
private static function getExtendData($exception)
|
|
||||||
{
|
|
||||||
$data = [];
|
|
||||||
if ($exception instanceof Exception) {
|
|
||||||
$data = $exception->getData();
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,11 +17,6 @@ namespace think;
|
|||||||
*/
|
*/
|
||||||
class Exception extends \Exception
|
class Exception extends \Exception
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* 系统异常后发送给客户端的HTTP Status
|
|
||||||
* @var integer
|
|
||||||
*/
|
|
||||||
protected $httpStatus = 500;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存异常页面显示的额外Debug数据
|
* 保存异常页面显示的额外Debug数据
|
||||||
@@ -43,7 +38,7 @@ class Exception extends \Exception
|
|||||||
* key2 value2
|
* key2 value2
|
||||||
*
|
*
|
||||||
* @param string $label 数据分类,用于异常页面显示
|
* @param string $label 数据分类,用于异常页面显示
|
||||||
* @param Array $data 需要显示的数据,必须为关联数组
|
* @param array $data 需要显示的数据,必须为关联数组
|
||||||
*/
|
*/
|
||||||
final protected function setData($label, array $data)
|
final protected function setData($label, array $data)
|
||||||
{
|
{
|
||||||
@@ -60,12 +55,4 @@ class Exception extends \Exception
|
|||||||
return $this->data;
|
return $this->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取要发送给客户端的HTTP Status
|
|
||||||
* @return integer HTTP Status
|
|
||||||
*/
|
|
||||||
final public function getHttpStatus()
|
|
||||||
{
|
|
||||||
return $this->httpStatus;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,10 +95,20 @@ class Log
|
|||||||
*/
|
*/
|
||||||
public static function save()
|
public static function save()
|
||||||
{
|
{
|
||||||
|
if (!empty(self::$log)) {
|
||||||
if (is_null(self::$driver)) {
|
if (is_null(self::$driver)) {
|
||||||
self::init(Config::get('log'));
|
self::init(Config::get('log'));
|
||||||
}
|
}
|
||||||
return self::$driver->save(self::$log);
|
|
||||||
|
$result = self::$driver->save(self::$log);
|
||||||
|
|
||||||
|
if ($result) {
|
||||||
|
self::$log = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class Response
|
|||||||
* 初始化
|
* 初始化
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $type 输出类型
|
* @param string $type 输出类型
|
||||||
* @return \think\Request
|
* @return \think\Response
|
||||||
*/
|
*/
|
||||||
public static function instance($type = '')
|
public static function instance($type = '')
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -18,6 +18,15 @@ use think\exception\DbException;
|
|||||||
*/
|
*/
|
||||||
class DbBindParamException extends DbException
|
class DbBindParamException extends DbException
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DbBindParamException constructor.
|
||||||
|
* @param string $message
|
||||||
|
* @param array $config
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $bind
|
||||||
|
* @param int $code
|
||||||
|
*/
|
||||||
public function __construct($message, $config, $sql, $bind, $code = 10502)
|
public function __construct($message, $config, $sql, $bind, $code = 10502)
|
||||||
{
|
{
|
||||||
$this->setData('Bind Param', $bind);
|
$this->setData('Bind Param', $bind);
|
||||||
|
|||||||
222
library/think/exception/Handle.php
Normal file
222
library/think/exception/Handle.php
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
<?php
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Author: yunwuxin <448901948@qq.com>
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace think\exception;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use think\Config;
|
||||||
|
use think\Log;
|
||||||
|
use think\Response;
|
||||||
|
|
||||||
|
class Handle
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $ignoreReport = [
|
||||||
|
'\\think\\exception\\HttpException'
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Report or log an exception.
|
||||||
|
*
|
||||||
|
* @param \Exception $exception
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function report(Exception $exception)
|
||||||
|
{
|
||||||
|
if (!$this->isIgnoreReport($exception)) {
|
||||||
|
// 收集异常数据
|
||||||
|
if (APP_DEBUG) {
|
||||||
|
$data = [
|
||||||
|
'file' => $exception->getFile(),
|
||||||
|
'line' => $exception->getLine(),
|
||||||
|
'message' => $exception->getMessage(),
|
||||||
|
'code' => $this->getCode($exception)
|
||||||
|
];
|
||||||
|
$log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
|
||||||
|
} else {
|
||||||
|
$data = [
|
||||||
|
'code' => $exception->getCode(),
|
||||||
|
'message' => $exception->getMessage(),
|
||||||
|
];
|
||||||
|
$log = "[{$data['code']}]{$data['message']}";
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::record($log, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function isIgnoreReport(Exception $exception)
|
||||||
|
{
|
||||||
|
foreach ($this->ignoreReport as $class) {
|
||||||
|
if ($exception instanceof $class) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render an exception into an HTTP response.
|
||||||
|
*
|
||||||
|
* @param \Exception $e
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public function render(Exception $e)
|
||||||
|
{
|
||||||
|
if ($e instanceof HttpException) {
|
||||||
|
return $this->renderHttpException($e);
|
||||||
|
} else {
|
||||||
|
return $this->convertExceptionToResponse($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param HttpException $e
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
protected function renderHttpException(HttpException $e)
|
||||||
|
{
|
||||||
|
$status = $e->getStatusCode();
|
||||||
|
|
||||||
|
//TODO 根据状态码自动输出错误页面
|
||||||
|
|
||||||
|
return $this->convertExceptionToResponse($e);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Exception $exception
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
protected function convertExceptionToResponse(Exception $exception)
|
||||||
|
{
|
||||||
|
// 收集异常数据
|
||||||
|
if (APP_DEBUG) {
|
||||||
|
// 调试模式,获取详细的错误信息
|
||||||
|
$data = [
|
||||||
|
'name' => get_class($exception),
|
||||||
|
'file' => $exception->getFile(),
|
||||||
|
'line' => $exception->getLine(),
|
||||||
|
'message' => $exception->getMessage(),
|
||||||
|
'trace' => $exception->getTrace(),
|
||||||
|
'code' => $this->getCode($exception),
|
||||||
|
'source' => $this->getSourceCode($exception),
|
||||||
|
'datas' => $this->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' => $this->getConst(),
|
||||||
|
]
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
// 部署模式仅显示 Code 和 Message
|
||||||
|
$data = [
|
||||||
|
'code' => $exception->getCode(),
|
||||||
|
'message' => $exception->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!APP_DEBUG && !Config::get('show_error_msg')) {
|
||||||
|
// 不显示详细错误信息
|
||||||
|
$data['message'] = Config::get('error_message');
|
||||||
|
}
|
||||||
|
ob_start();
|
||||||
|
ob_implicit_flush(0);
|
||||||
|
extract($data);
|
||||||
|
include Config::get('exception_tmpl');
|
||||||
|
// 获取并清空缓存
|
||||||
|
$content = ob_get_clean();
|
||||||
|
|
||||||
|
$response = Response::instance()->data($content);
|
||||||
|
|
||||||
|
if ($exception instanceof HttpException) {
|
||||||
|
$statusCode = $exception->getStatusCode();
|
||||||
|
//TODO 设置headers 等待response完善
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($statusCode)) {
|
||||||
|
$statusCode = 500;
|
||||||
|
}
|
||||||
|
$response->code($statusCode);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取错误编码
|
||||||
|
* ErrorException则使用错误级别作为错误编码
|
||||||
|
* @param \Exception $exception
|
||||||
|
* @return integer 错误编码
|
||||||
|
*/
|
||||||
|
protected function getCode(Exception $exception)
|
||||||
|
{
|
||||||
|
$code = $exception->getCode();
|
||||||
|
if (!$code && $exception instanceof ErrorException) {
|
||||||
|
$code = $exception->getSeverity();
|
||||||
|
}
|
||||||
|
return $code;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取出错文件内容
|
||||||
|
* 获取错误的前9行和后9行
|
||||||
|
* @param \Exception $exception
|
||||||
|
* @return array 错误文件内容
|
||||||
|
*/
|
||||||
|
protected function getSourceCode(Exception $exception)
|
||||||
|
{
|
||||||
|
// 读取前9行和后9行
|
||||||
|
$line = $exception->getLine();
|
||||||
|
$first = ($line - 9 > 0) ? $line - 9 : 1;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$contents = file($exception->getFile());
|
||||||
|
$source = [
|
||||||
|
'first' => $first,
|
||||||
|
'source' => array_slice($contents, $first - 1, 19),
|
||||||
|
];
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$source = [];
|
||||||
|
}
|
||||||
|
return $source;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取异常扩展信息
|
||||||
|
* 用于非调试模式html返回类型显示
|
||||||
|
* @param \Exception $exception
|
||||||
|
* @return array 异常类定义的扩展数据
|
||||||
|
*/
|
||||||
|
protected function getExtendData(Exception $exception)
|
||||||
|
{
|
||||||
|
$data = [];
|
||||||
|
if ($exception instanceof \think\Exception) {
|
||||||
|
$data = $exception->getData();
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取常量列表
|
||||||
|
* @return array 常量列表
|
||||||
|
*/
|
||||||
|
private static function getConst()
|
||||||
|
{
|
||||||
|
return get_defined_constants(true)['user'];
|
||||||
|
}
|
||||||
|
}
|
||||||
37
library/think/exception/HttpException.php
Normal file
37
library/think/exception/HttpException.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Author: yunwuxin <448901948@qq.com>
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace think\exception;
|
||||||
|
|
||||||
|
|
||||||
|
class HttpException extends \RuntimeException
|
||||||
|
{
|
||||||
|
private $statusCode;
|
||||||
|
private $headers;
|
||||||
|
|
||||||
|
public function __construct($statusCode, $message = null, \Exception $previous = null, array $headers = [], $code = 0)
|
||||||
|
{
|
||||||
|
$this->statusCode = $statusCode;
|
||||||
|
$this->headers = $headers;
|
||||||
|
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getStatusCode()
|
||||||
|
{
|
||||||
|
return $this->statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getHeaders()
|
||||||
|
{
|
||||||
|
return $this->headers;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,27 +1,35 @@
|
|||||||
<?php
|
<?php
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
|
// | Author: yunwuxin <448901948@qq.com>
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
namespace think\exception;
|
namespace think\exception;
|
||||||
|
|
||||||
use think\Exception;
|
|
||||||
|
|
||||||
/**
|
use think\Response;
|
||||||
* Database相关异常处理类
|
|
||||||
*/
|
class HttpResponseException extends \RuntimeException
|
||||||
class NotFoundException extends Exception
|
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* 系统异常后发送给客户端的HTTP Status
|
* @var Response
|
||||||
* @var integer
|
|
||||||
*/
|
*/
|
||||||
protected $httpStatus = 404;
|
protected $response;
|
||||||
|
|
||||||
|
public function __construct(Response $response)
|
||||||
|
{
|
||||||
|
$this->response = $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getResponse()
|
||||||
|
{
|
||||||
|
return $this->response;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
49
library/think/exception/ThrowableError.php
Normal file
49
library/think/exception/ThrowableError.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Author: yunwuxin <448901948@qq.com>
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace think\exception;
|
||||||
|
|
||||||
|
|
||||||
|
class ThrowableError extends \ErrorException
|
||||||
|
{
|
||||||
|
public function __construct(\Throwable $e)
|
||||||
|
{
|
||||||
|
|
||||||
|
if ($e instanceof \ParseError) {
|
||||||
|
$message = 'Parse error: ' . $e->getMessage();
|
||||||
|
$severity = E_PARSE;
|
||||||
|
} elseif ($e instanceof \TypeError) {
|
||||||
|
$message = 'Type error: ' . $e->getMessage();
|
||||||
|
$severity = E_RECOVERABLE_ERROR;
|
||||||
|
} else {
|
||||||
|
$message = 'Fatal error: ' . $e->getMessage();
|
||||||
|
$severity = E_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::__construct(
|
||||||
|
$message,
|
||||||
|
$e->getCode(),
|
||||||
|
$severity,
|
||||||
|
$e->getFile(),
|
||||||
|
$e->getLine()
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
$this->setTrace($e->getTrace());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setTrace($trace)
|
||||||
|
{
|
||||||
|
$traceReflector = new \ReflectionProperty('Exception', 'trace');
|
||||||
|
$traceReflector->setAccessible(true);
|
||||||
|
$traceReflector->setValue($this, $trace);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user