类 ExceptionTrap
CakePHP 异常处理的入口点。
使用 register() 方法,你可以将 ExceptionTrap 附加到 PHP 的默认异常处理程序,并注册一个关闭处理程序来处理致命错误。
当异常被捕获时,Exception.beforeRender 事件会被触发。然后,异常会被记录(如果已启用),最后使用定义的渲染器进行“渲染”。
停止 Exception.beforeRender 事件不会有任何影响,因为我们始终需要对异常渲染响应,并且如果你想替换或跳过渲染异常,则应使用自定义渲染器。
如果未定义,异常渲染器将根据当前 SAPI(CLI 或 Web)进行选择。
属性摘要
-
$_config protected
array<string, mixed>运行时配置
-
$_configInitialized protected
bool配置属性是否已使用默认值进行配置
-
$_defaultConfig protected
array<string, mixed>配置选项。通常,这些选项将在你的 config/app.php 中定义。
-
$_eventClass protected
string新事件对象的默认类名。
-
$_eventManager protected
Cake\Event\EventManagerInterface|nullCake\Event\EventManager 的实例,该对象用于分发内部事件。
-
$callbacks protected
arrayClosure>处理回调的列表。
-
$disabled protected
bool跟踪此陷阱是否已从全局处理程序中删除。
-
$registeredTrap protected static
Cake\Error\ExceptionTrap|null当前注册的全局异常处理程序
方法摘要
-
__construct() public
构造函数
-
_configDelete() protected
删除单个配置键。
-
_configRead() protected
读取配置键。
-
_configWrite() protected
写入配置键。
-
chooseRenderer() protected
根据配置或 SAPI 选择异常渲染器
-
configShallow() public
将提供的配置与现有配置合并。与
config()方法不同,config()方法对嵌套键进行递归合并,而此方法进行简单合并。 -
dispatchEvent() public
用于创建和分发事件的包装器。
-
getConfig() public
返回配置。
-
getConfigOrFail() public
返回此特定键的配置。
-
getEventManager() public
返回此对象的 Cake\Event\EventManager 管理器实例。
-
handleException() public
处理未捕获的异常。
-
handleFatalError() public
显示/记录致命错误。
-
handleShutdown() public
关闭处理程序
-
increaseMemoryLimit() public
将 PHP 的“memory_limit” ini 设置增加指定的千字节数
-
instance() public static
获取已设置的注册全局实例。
-
logException() public
记录异常。
-
logInternalError() public
触发在渲染异常期间发生的错误。
-
logger() public
获取日志记录器的实例。
-
register() public
将此 ExceptionTrap 附加到 PHP 的默认异常处理程序。
-
renderer() public
获取渲染器的实例。
-
setConfig() public
设置配置。
-
setEventManager() public
返回此对象的 Cake\Event\EventManagerInterface 实例。
-
unregister() public
从单例中删除此实例
方法详细说明
__construct() ¶ public
__construct(array<string, mixed> $options = [])
构造函数
参数
-
array<string, mixed>$options optional 选项数组。请参见 $_defaultConfig。
_configDelete() ¶ protected
_configDelete(string $key): void
删除单个配置键。
参数
-
string$key 要删除的键。
返回
void抛出
Cake\Core\Exception\CakeException如果尝试覆盖现有配置
_configRead() ¶ protected
_configRead(string|null $key): mixed
读取配置键。
参数
-
string|null$key 要读取的键。
返回
mixed_configWrite() ¶ protected
_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void
写入配置键。
参数
-
array<string, mixed>|string$key 要写入的键。
-
mixed$value 要写入的值。
-
string|bool$merge optional 如果为 true,则递归合并,如果为“shallow”,则简单合并,如果为 false,则覆盖,默认为 false。
返回
void抛出
Cake\Core\Exception\CakeException如果尝试覆盖现有配置
chooseRenderer() ¶ protected
chooseRenderer(): class-stringCake\Error\ExceptionRendererInterface>
根据配置或 SAPI 选择异常渲染器
返回
class-stringCake\Error\ExceptionRendererInterface>configShallow() ¶ public
configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this
将提供的配置与现有配置合并。与 config() 方法不同,config() 方法对嵌套键进行递归合并,而此方法进行简单合并。
设置特定值
$this->configShallow('key', $value);
设置嵌套值
$this->configShallow('some.nested.key', $value);
同时更新多个配置设置
$this->configShallow(['one' => 'value', 'another' => 'value']);
参数
-
array<string, mixed>|string$key 要设置的键,或完整的配置数组。
-
mixed|null$value optional 要设置的值。
返回
$thisdispatchEvent() ¶ public
dispatchEvent(string $name, array $data = [], TSubject|null $subject = null): Cake\Event\EventInterface<TSubject>
用于创建和分发事件的包装器。
返回分发的事件。
参数
-
string$name 事件名称。
-
array$data optional 你希望与该事件一起传输的任何值,监听器都可以读取这些值。
-
TSubject|null$subject optional 此事件所适用的对象(默认情况下为 $this)。
返回
Cake\Event\EventInterface<TSubject>getConfig() ¶ public
getConfig(string|null $key = null, mixed $default = null): mixed
返回配置。
用法
读取整个配置
$this->getConfig();
读取特定值
$this->getConfig('key');
读取嵌套值
$this->getConfig('some.nested.key');
使用默认值读取
$this->getConfig('some-key', 'default-value');
参数
-
string|null$key optional 要获取的键,或整个配置的 null。
-
mixed$default optional 键不存在时的返回值。
返回
mixedgetConfigOrFail() ¶ public
getConfigOrFail(string $key): mixed
返回此特定键的配置。
此键的配置值必须存在,不能为 null。
参数
-
string$key 要获取的键。
返回
mixed抛出
InvalidArgumentExceptiongetEventManager() ¶ public
getEventManager(): Cake\Event\EventManagerInterface
返回此对象的 Cake\Event\EventManager 管理器实例。
你可以使用此实例向对象事件注册任何新的监听器或回调,或者创建你自己的事件并按需触发它们。
返回
Cake\Event\EventManagerInterfacehandleException() ¶ public
handleException(Throwable $exception): void
处理未捕获的异常。
使用子类提供的模板方法以环境适当的方式显示错误。
参数
-
Throwable$exception 异常实例。
返回
void抛出
Exception当渲染器类未找到时
另请参见
handleFatalError() ¶ public
handleFatalError(int $code, string $description, string $file, int $line): void
显示/记录致命错误。
参数
-
int$code 错误代码
-
string$description 错误描述
-
string$file 发生错误的文件
-
int$line 触发错误的行号
返回
voidincreaseMemoryLimit() ¶ public
increaseMemoryLimit(int $additionalKb): void
将 PHP 的“memory_limit” ini 设置增加指定的千字节数
参数
-
int$additionalKb 千字节数
返回
voidinstance() ¶ public static
instance(): Cake\Error\ExceptionTrap|null
获取已设置的注册全局实例。
请记住,此处的全局状态是可变的,此方法返回的对象可能是过时的值。
返回
Cake\Error\ExceptionTrap|nulllogException() ¶ public
logException(Throwable $exception, Psr\Http\Message\ServerRequestInterface|null $request = null): void
记录异常。
主要是一个公共函数,以确保全局异常处理和 ErrorHandlerMiddleware 之间的一致性。此方法将应用 skipLog 过滤器,如果异常不应该记录,则跳过记录。
在尝试记录后,会触发 Exception.beforeRender 事件。
参数
-
Throwable$exception 要记录的异常
-
Psr\Http\Message\ServerRequestInterface|null$request optional 可选的请求
返回
voidlogInternalError() ¶ public
logInternalError(Throwable $exception): void
触发在渲染异常期间发生的错误。
通过触发 E_USER_ERROR,我们最终可以进入默认的异常处理,这将记录渲染失败,并希望渲染一个错误页面。
参数
-
Throwable$exception 要记录的异常
返回
voidlogger() ¶ public
logger(): Cake\Error\ErrorLoggerInterface
获取日志记录器的实例。
返回
Cake\Error\ErrorLoggerInterfaceregister() ¶ public
register(): void
将此 ExceptionTrap 附加到 PHP 的默认异常处理程序。
这将替换现有的异常处理程序,之前的异常处理程序将被丢弃。
返回
voidrenderer() ¶ public
renderer(Throwable $exception, Psr\Http\Message\ServerRequestInterface|null $request = null): Cake\Error\ExceptionRendererInterface
获取渲染器的实例。
参数
-
Throwable$exception 要渲染的异常
-
Psr\Http\Message\ServerRequestInterface|null$request optional 如果可能,请求。
返回
Cake\Error\ExceptionRendererInterfacesetConfig() ¶ public
setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this
设置配置。
用法
设置特定值
$this->setConfig('key', $value);
设置嵌套值
$this->setConfig('some.nested.key', $value);
同时更新多个配置设置
$this->setConfig(['one' => 'value', 'another' => 'value']);
参数
-
array<string, mixed>|string$key 要设置的键,或完整的配置数组。
-
mixed|null$value optional 要设置的值。
-
bool$merge optional 是否递归合并或覆盖现有配置,默认为 true。
返回
$this抛出
Cake\Core\Exception\CakeException当试图设置一个无效的键时。
setEventManager() ¶ public
setEventManager(Cake\Event\EventManagerInterface $eventManager): $this
返回此对象的 Cake\Event\EventManagerInterface 实例。
你可以使用此实例向对象事件注册任何新的监听器或回调,或者创建你自己的事件并按需触发它们。
参数
-
Cake\Event\EventManagerInterface$eventManager 要设置的 eventManager
返回
$this属性详情
$_defaultConfig ¶ protected
配置选项。通常,这些选项将在你的 config/app.php 中定义。
exceptionRenderer- string - 负责渲染未捕获异常的类。所选类将用于 CLI 和 Web 环境。如果您希望在 CLI 和 Web 环境中使用不同的类,则需要编写该条件逻辑。log设置为 false 以禁用日志记录。logger- string - 要使用的错误记录器的类名。trace- boolean - 是否应在记录的异常中包含回溯。skipLog- array - 要跳过日志记录的异常列表。扩展列出异常之一的异常也将不会被记录。例如:'skipLog' => ['Cake\Http\Exception\NotFoundException', 'Cake\Http\Exception\UnauthorizedException']此选项将转发到配置的
loggerextraFatalErrorMemory- int - 遇到致命错误时要增加的内存限制的兆字节数。这允许有足够的喘息空间来完成日志记录或错误处理。stderr用于控制台环境,以便渲染器可以访问当前控制台输出流。
类型
array<string, mixed>$_eventManager ¶ protected
Cake\Event\EventManager 的实例,该对象用于分发内部事件。
类型
Cake\Event\EventManagerInterface|null$registeredTrap ¶ protected static
当前注册的全局异常处理程序
这是尽力而为,因为我们不知道是否/何时注册另一个异常处理程序。
类型
Cake\Error\ExceptionTrap|null