ErrorTrap
in package
uses
EventDispatcherTrait, InstanceConfigTrait
Entry point to CakePHP's error handling.
Using the register()
method you can attach an ErrorTrap to PHP's default error handler.
When errors are trapped, errors are logged (if logging is enabled). Then the Error.beforeRender
event is triggered.
Finally, errors are 'rendered' using the defined renderer. If no error renderer is defined in configuration
one of the default implementations will be chosen based on the PHP SAPI.
Table of Contents
Properties
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_defaultConfig : array<string, mixed>
- Configuration options. Generally these are defined in config/app.php
- $_eventClass : string
- Default class name for new event objects.
- $_eventManager : EventManagerInterface|null
- Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
Methods
- __construct() : mixed
- Constructor
- configShallow() : $this
- Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
- dispatchEvent() : EventInterface
- Wrapper for creating and dispatching events.
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- getEventManager() : EventManagerInterface
- Returns the Cake\Event\EventManager manager instance for this object.
- handleError() : bool
- Handle an error from PHP set_error_handler
- logger() : ErrorLoggerInterface
- Get an instance of the logger.
- register() : void
- Attach this ErrorTrap to PHP's default error handler.
- renderer() : ErrorRendererInterface
- Get an instance of the renderer.
- setConfig() : $this
- Sets the config.
- setEventManager() : $this
- Returns the Cake\Event\EventManagerInterface instance for this object.
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- chooseErrorRenderer() : ErrorRendererInterface>
- Choose an error renderer based on config or the SAPI
- logError() : void
- Logging helper method.
Properties
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_defaultConfig
Configuration options. Generally these are defined in config/app.php
protected
array<string, mixed>
$_defaultConfig
= ['errorLevel' => E_ALL, 'errorRenderer' => null, 'log' => true, 'logger' => \Cake\Error\ErrorLogger::class, 'trace' => false]
-
errorLevel
- int - The level of errors you are interested in capturing. -
errorRenderer
- string - The class name of render errors with. Defaults to choosing between Html and Console based on the SAPI. -
log
- boolean - Whether or not you want errors logged. -
logger
- string - The class name of the error logger to use. -
trace
- boolean - Whether or not backtraces should be included in logged errors.
$_eventClass
Default class name for new event objects.
protected
string
$_eventClass
= \Cake\Event\Event::class
$_eventManager
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
protected
EventManagerInterface|null
$_eventManager
Methods
__construct()
Constructor
public
__construct([array<string, mixed> $options = [] ]) : mixed
Parameters
- $options : array<string, mixed> = []
-
An options array. See $_defaultConfig.
configShallow()
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thisdispatchEvent()
Wrapper for creating and dispatching events.
public
dispatchEvent(string $name[, array<string|int, mixed>|null $data = null ][, object|null $subject = null ]) : EventInterface
Returns a dispatched event.
Parameters
- $name : string
-
Name of the event.
- $data : array<string|int, mixed>|null = null
-
Any value you wish to be transported with this event to it can be read by listeners.
- $subject : object|null = null
-
The object that this event applies to ($this by default).
Return values
EventInterfacegetConfig()
Returns the config.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Returns the config for this specific key.
public
getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
getEventManager()
Returns the Cake\Event\EventManager manager instance for this object.
public
getEventManager() : EventManagerInterface
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
Return values
EventManagerInterfacehandleError()
Handle an error from PHP set_error_handler
public
handleError(int $code, string $description[, string|null $file = null ][, int|null $line = null ]) : bool
Will use the configured renderer to generate output and output it.
This method will dispatch the Error.beforeRender
event which can be listened
to on the global event manager.
Parameters
- $code : int
-
Code of error
- $description : string
-
Error description
- $file : string|null = null
-
File on which error occurred
- $line : int|null = null
-
Line that triggered the error
Return values
bool —True if error was handled
logger()
Get an instance of the logger.
public
logger() : ErrorLoggerInterface
Return values
ErrorLoggerInterfaceregister()
Attach this ErrorTrap to PHP's default error handler.
public
register() : void
This will replace the existing error handler, and the previous error handler will be discarded.
This method will also set the global error level via error_reporting().
renderer()
Get an instance of the renderer.
public
renderer() : ErrorRendererInterface
Return values
ErrorRendererInterfacesetConfig()
Sets the config.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$thissetEventManager()
Returns the Cake\Event\EventManagerInterface instance for this object.
public
setEventManager(EventManagerInterface $eventManager) : $this
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
Parameters
- $eventManager : EventManagerInterface
-
the eventManager to set
Return values
$this_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Tags
chooseErrorRenderer()
Choose an error renderer based on config or the SAPI
protected
chooseErrorRenderer() : ErrorRendererInterface>
Return values
ErrorRendererInterface>logError()
Logging helper method.
protected
logError(PhpError $error) : void
Parameters
- $error : PhpError
-
The error object to log.