Fr3nch13/CakePHP Utilities

BaseErrorHandler
in package
uses InstanceConfigTrait

AbstractYes

Base error handler that provides logic common to the CLI + web error/exception handlers.

Subclasses are required to implement the template methods to handle displaying the errors in their environment.

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>
Options to use for the Error handling.
$_handled  : bool
$logger  : ErrorLoggerInterface|null
Exception logger instance.

Methods

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.
getLogger()  : ErrorLoggerInterface
Get exception logger.
handleError()  : bool
Set as the default error handler by CakePHP.
handleException()  : void
Handle uncaught exceptions.
handleFatalError()  : bool
Display/Log a fatal error.
increaseMemoryLimit()  : void
Increases the PHP "memory_limit" ini setting by the specified amount in kilobytes
logException()  : bool
Log an error for the exception if applicable.
mapErrorCode()  : array<string|int, mixed>
Map an error code into an Error word, and log location.
register()  : void
Register the error and exception handlers.
setConfig()  : $this
Sets the config.
wrapAndHandleException()  : void
Checks the passed exception type. If it is an instance of `Error` then, it wraps the passed object inside another Exception object for backwards compatibility purposes.
_configDelete()  : void
Deletes a single config key.
_configRead()  : mixed
Reads a config key.
_configWrite()  : void
Writes a config key.
_displayError()  : void
Display an error message in an environment specific way.
_displayException()  : void
Display an exception in an environment specific way.
_logError()  : bool
Log an error.
_stop()  : void
Stop the process.

Properties

$_configInitialized

Whether the config property has already been configured with defaults

protected bool $_configInitialized = false

$_defaultConfig

Options to use for the Error handling.

protected array<string, mixed> $_defaultConfig = ['log' => true, 'trace' => false, 'skipLog' => [], 'errorLogger' => \Cake\Error\ErrorLogger::class]

Methods

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
$this

getConfig()

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
throws
InvalidArgumentException
Return values
mixed

Configuration data at the named key

handleError()

Set as the default error handler by CakePHP.

public handleError(int $code, string $description[, string|null $file = null ][, int|null $line = null ][, array<string, mixed>|null $context = null ]) : bool

Use config/error.php to customize or replace this error handler. This function will use Debugger to display errors when debug mode is on. And will log errors to Log, when debug mode is off.

You can use the 'errorLevel' option to set what type of errors will be handled. Stack traces for errors can be enabled with the 'trace' option.

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

$context : array<string, mixed>|null = null

Context

Return values
bool

True if error was handled

handleFatalError()

Display/Log a fatal error.

public handleFatalError(int $code, string $description, string $file, int $line) : bool
Parameters
$code : int

Code of error

$description : string

Error description

$file : string

File on which error occurred

$line : int

Line that triggered the error

Return values
bool

increaseMemoryLimit()

Increases the PHP "memory_limit" ini setting by the specified amount in kilobytes

public increaseMemoryLimit(int $additionalKb) : void
Parameters
$additionalKb : int

Number in kilobytes

logException()

Log an error for the exception if applicable.

public logException(Throwable $exception[, ServerRequestInterface|null $request = null ]) : bool
Parameters
$exception : Throwable

The exception to log a message for.

$request : ServerRequestInterface|null = null

The current request.

Return values
bool

mapErrorCode()

Map an error code into an Error word, and log location.

public static mapErrorCode(int $code) : array<string|int, mixed>
Parameters
$code : int

Error code to map

Return values
array<string|int, mixed>

Array of error word, and log location.

register()

Register the error and exception handlers.

public register() : void

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
throws
CakeException

When trying to set a key that is invalid.

Return values
$this

wrapAndHandleException()

Checks the passed exception type. If it is an instance of `Error` then, it wraps the passed object inside another Exception object for backwards compatibility purposes.

public wrapAndHandleException(Throwable $exception) : void

Unused method will be removed in 5.0

Parameters
$exception : Throwable

The exception to handle

_configDelete()

Deletes a single config key.

protected _configDelete(string $key) : void
Parameters
$key : string

Key to delete.

Tags
throws
CakeException

if attempting to clobber existing config

_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
throws
CakeException

if attempting to clobber existing config

_displayError()

Display an error message in an environment specific way.

protected abstract _displayError(array<string|int, mixed> $error, bool $debug) : void

Subclasses should implement this method to display the error as desired for the runtime they operate in.

Parameters
$error : array<string|int, mixed>

An array of error data.

$debug : bool

Whether the app is in debug mode.

_displayException()

Display an exception in an environment specific way.

protected abstract _displayException(Throwable $exception) : void

Subclasses should implement this method to display an uncaught exception as desired for the runtime they operate in.

Parameters
$exception : Throwable

The uncaught exception.

_logError()

Log an error.

protected _logError(string|int $level, array<string|int, mixed> $data) : bool
Parameters
$level : string|int

The level name of the log.

$data : array<string|int, mixed>

Array of error data.

Return values
bool

_stop()

Stop the process.

protected _stop(int $code) : void

Implemented in subclasses that need it.

Parameters
$code : int

Exit code.


        
On this page

Search results