mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 07:02:47 +08:00
完善框架的异常处理
This commit is contained in:
@@ -18,6 +18,15 @@ use think\exception\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)
|
||||
{
|
||||
$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
|
||||
// +----------------------------------------------------------------------
|
||||
// | 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 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\exception;
|
||||
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* Database相关异常处理类
|
||||
*/
|
||||
class NotFoundException extends Exception
|
||||
use think\Response;
|
||||
|
||||
class HttpResponseException extends \RuntimeException
|
||||
{
|
||||
/**
|
||||
* 系统异常后发送给客户端的HTTP Status
|
||||
* @var integer
|
||||
* @var Response
|
||||
*/
|
||||
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