ErrorHandlerMiddleware
in package
implements
MiddlewareInterface
uses
InstanceConfigTrait
Error handling middleware.
Traps exceptions and converts them into HTML or content-type appropriate error pages using the CakePHP ExceptionRenderer.
Table of Contents
Interfaces
- MiddlewareInterface
- Participant in processing a server request and response.
Properties
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_defaultConfig : array<string, mixed>
- Default configuration values.
- $errorHandler : ErrorHandler|null
- Error handler instance.
- $exceptionTrap : ExceptionTrap|null
- ExceptionTrap instance
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.
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- handleException() : ResponseInterface
- Handle an exception and generate an error response
- handleRedirect() : ResponseInterface
- Convert a redirect exception into a response.
- process() : ResponseInterface
- Wrap the remaining middleware with error handling.
- setConfig() : $this
- Sets the config.
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- getErrorHandler() : ErrorHandler
- Get a error handler instance
- getExceptionTrap() : ExceptionTrap
- Get a exception trap instance
- handleInternalError() : ResponseInterface
- Handle internal errors.
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
Default configuration values.
protected
array<string, mixed>
$_defaultConfig
= ['exceptionRenderer' => \Cake\Error\Renderer\WebExceptionRenderer::class]
Ignored if contructor is passed an ExceptionTrap instance.
Configuration keys and values are shared with ExceptionTrap
.
This class will pass its configuration onto the ExceptionTrap
class if you are using the array style constructor.
Tags
$errorHandler
Error handler instance.
protected
ErrorHandler|null
$errorHandler
= null
$exceptionTrap
ExceptionTrap instance
protected
ExceptionTrap|null
$exceptionTrap
= null
Methods
__construct()
Constructor
public
__construct([ErrorHandler|ExceptionTrap|array<string|int, mixed> $errorHandler = [] ]) : mixed
Parameters
- $errorHandler : ErrorHandler|ExceptionTrap|array<string|int, mixed> = []
-
The error handler instance or config array.
Tags
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
$thisgetConfig()
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
handleException()
Handle an exception and generate an error response
public
handleException(Throwable $exception, ServerRequestInterface $request) : ResponseInterface
Parameters
- $exception : Throwable
-
The exception to handle.
- $request : ServerRequestInterface
-
The request.
Return values
ResponseInterface —A response.
handleRedirect()
Convert a redirect exception into a response.
public
handleRedirect(RedirectException $exception) : ResponseInterface
Parameters
- $exception : RedirectException
-
The exception to handle
Return values
ResponseInterface —Response created from the redirect.
process()
Wrap the remaining middleware with error handling.
public
process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
- $request : ServerRequestInterface
-
The request.
- $handler : RequestHandlerInterface
-
The request handler.
Return values
ResponseInterface —A response
setConfig()
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
$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
getErrorHandler()
Get a error handler instance
protected
getErrorHandler() : ErrorHandler
Return values
ErrorHandler —The error handler.
getExceptionTrap()
Get a exception trap instance
protected
getExceptionTrap() : ExceptionTrap
Return values
ExceptionTrap —The exception trap.
handleInternalError()
Handle internal errors.
protected
handleInternalError() : ResponseInterface
Return values
ResponseInterface —A response