Files
cunkebao_v3/Server/thinkphp/library/think/Error.php

137 lines
3.8 KiB
PHP
Raw Normal View History

2025-04-02 16:38:48 +08:00
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
// +----------------------------------------------------------------------
namespace think;
use think\console\Output as ConsoleOutput;
use think\exception\ErrorException;
use think\exception\Handle;
use think\exception\ThrowableError;
class Error
{
/**
* 注册异常处理
* @access public
* @return void
*/
public static function register()
{
error_reporting(E_ALL);
set_error_handler([__CLASS__, 'appError']);
set_exception_handler([__CLASS__, 'appException']);
register_shutdown_function([__CLASS__, 'appShutdown']);
}
/**
2025-07-07 11:31:25 +08:00
* 异常处理
2025-04-02 16:38:48 +08:00
* @access public
2025-07-07 11:31:25 +08:00
* @param \Exception|\Throwable $e 异常
* @return void
2025-04-02 16:38:48 +08:00
*/
public static function appException($e)
{
if (!$e instanceof \Exception) {
$e = new ThrowableError($e);
}
2025-07-07 11:31:25 +08:00
$handler = self::getExceptionHandler();
$handler->report($e);
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
if (IS_CLI) {
$handler->renderForConsole(new ConsoleOutput, $e);
2025-04-02 16:38:48 +08:00
} else {
2025-07-07 11:31:25 +08:00
$handler->render($e)->send();
2025-04-02 16:38:48 +08:00
}
}
/**
2025-07-07 11:31:25 +08:00
* 错误处理
2025-04-02 16:38:48 +08:00
* @access public
2025-07-07 11:31:25 +08:00
* @param integer $errno 错误编号
* @param integer $errstr 详细错误信息
* @param string $errfile 出错的文件
* @param integer $errline 出错行号
* @return void
2025-04-02 16:38:48 +08:00
* @throws ErrorException
*/
public static function appError($errno, $errstr, $errfile = '', $errline = 0)
{
$exception = new ErrorException($errno, $errstr, $errfile, $errline);
2025-07-07 11:31:25 +08:00
// 符合异常处理的则将错误信息托管至 think\exception\ErrorException
2025-04-02 16:38:48 +08:00
if (error_reporting() & $errno) {
throw $exception;
}
self::getExceptionHandler()->report($exception);
}
/**
2025-07-07 11:31:25 +08:00
* 异常中止处理
2025-04-02 16:38:48 +08:00
* @access public
2025-07-07 11:31:25 +08:00
* @return void
2025-04-02 16:38:48 +08:00
*/
public static function appShutdown()
{
2025-07-07 11:31:25 +08:00
// 将错误信息托管至 think\ErrorException
2025-04-02 16:38:48 +08:00
if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {
2025-07-07 11:31:25 +08:00
self::appException(new ErrorException(
$error['type'], $error['message'], $error['file'], $error['line']
));
2025-04-02 16:38:48 +08:00
}
// 写入日志
2025-07-07 11:31:25 +08:00
Log::save();
2025-04-02 16:38:48 +08:00
}
/**
* 确定错误类型是否致命
* @access protected
2025-07-07 11:31:25 +08:00
* @param int $type 错误类型
2025-04-02 16:38:48 +08:00
* @return bool
*/
protected static function isFatal($type)
{
return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
}
/**
2025-07-07 11:31:25 +08:00
* 获取异常处理的实例
2025-04-02 16:38:48 +08:00
* @access public
* @return Handle
*/
public static function getExceptionHandler()
{
static $handle;
if (!$handle) {
2025-07-07 11:31:25 +08:00
// 异常处理 handle
$class = Config::get('exception_handle');
2025-04-02 16:38:48 +08:00
2025-07-07 11:31:25 +08:00
if ($class && is_string($class) && class_exists($class) &&
is_subclass_of($class, "\\think\\exception\\Handle")
) {
2025-04-02 16:38:48 +08:00
$handle = new $class;
} else {
$handle = new Handle;
2025-07-07 11:31:25 +08:00
2025-04-02 16:38:48 +08:00
if ($class instanceof \Closure) {
$handle->setRender($class);
}
2025-07-07 11:31:25 +08:00
2025-04-02 16:38:48 +08:00
}
}
return $handle;
}
}