Loading…

PHP ErrorHandler documentation in progress

Overview

Quick Start

Installable via Composer as bdk/errorhandler

composer.json
{
    "require": {
        "bdk/debug": "^2.0"
    }
}

Quality / Badges

  • Build Status
  • Maintainability
  • Code Coverage

Classes

ErrorHandler

__construct(EventManager $eventManager[, array $cfg = array()])

Constructor

Parameters
eventManager
event manager
cfg
config
errorReporting(): int

What error level are we handling

Return Value
int
get(string $key[, string $hash = null]): mixed

Retrieve a data value or property

Parameters
key
what to get
hash
if key == 'error', specify error hash
Return Value
mixed
static getInstance(array $cfg = array()): object|false

Returns the Singleton instance of this class (IF INSTANCE EXISTS)

Parameters
cfg
optional config
Return Value
object|false
getLastError(bool $inclSuppressed = false): Error|null

Get information about last error

Parameters
inclSuppressed
(false)
Return Value
Error|null
handleError(int $errType, string $errMsg, string $file, int $line[, array $vars = array()]): bool

Error handler

Parameters
errType
error level / type (one of PHP's E_* constants)
errMsg
the error message
file
filepath the error was raised in
line
the line the error was raised in
vars
active symbol table at point error occurred
Return Value
bool
false: will be handled by standard PHP error handler true: we "handled" / will not be handed by PHP error handler
phpDoc
handleException(Exception|Throwable $exception)

Handle uncaught exceptions

Description

This isn't strictly necessary... uncaught exceptions are a fatal error, which we can handle... However:

  • catching backtrace via shutdown function only possible if xdebug installed
  • xdebug_get_function_stack's magic seems powerless for uncaught exceptions!
Parameters
exception
exception to handle
onShutdown(Event $event)

EventManager::EVENT_PHP_SHUTDOWN event subscriber

Description

Used to handle fatal errors

Test fatal error handling by publishing EventManager::EVENT_PHP_SHUTDOWN event with error value

Parameters
event
EventManager::EVENT_PHP_SHUTDOWN event
register()

Register this error handler and shutdown function

setErrorCaller(array|null|false $caller = null[, int $offset = 0])

Set the calling file/line for next error.

Description

This override will apply until cleared or error occurs

Example: some wrapper function that is called often: Rather than reporting that an error occurred within the wrapper, you can use setErrorCaller() to report the error originating from the file/line that called the function

Parameters
caller
(default) null : determine automatically false or empty array: clear current value array() : manually set value
offset
(optional) if determining automatically : adjust how many frames to go back
unregister()

Un-register this error handler

Description

Restores previous error handler

Inherited from bdk\ErrorHandler\AbstractErrorHandler
setData(string $key, mixed $value)

Set data value

Parameters
key
what
value
value
Inherited from bdk\ErrorHandler\AbstractComponent
__get(string $prop): mixed

Magic getter

Description

Get inaccessible / undefined properties Lazy load child classes

Parameters
prop
property to get
Return Value
mixed
__isset(string $prop): bool

Magic isset

Parameters
prop
property to check
Return Value
bool
__set(string $prop, mixed $value)

Magic setter

Parameters
prop
Property name
value
Property value
phpDoc
throws: if key does not exist or is read only OutOfBoundsException
getCfg(array|string $path = null): mixed

Retrieve a configuration value

Parameters
path
(optional) what to get
Return Value
mixed
setCfg(array|string $mixed[, mixed $val = null]): mixed

Set one or more config values

Description
setCfg('key', 'value')
setCfg(array('k1'=>'v1', 'k2'=>'v2'))

Calls self::postSetCfg() with new values and previous values

Parameters
mixed
key=>value array or key
val
new value
Return Value
mixed
previous value(s)

Error

__construct(ErrorHandler $errHandler, array $values)

Constructor

Parameters
errHandler
ErrorHandler instance
values
Initial values must include type, message, file, & line optional: vars
asException(): Exception|ErrorException

Return as ErrorException (or caught exception)

Description

If error is an uncaught exception, the original Exception will be returned

Return Value
Exception|ErrorException
getBacktrace(bool|auto $withContext = auto): array|false|null

Alias for `getTrace()

Parameters
withContext
(auto) Whether to include code snippets
Return Value
array|false|null
getContext(): array|false

Get php code surrounding error

Return Value
array|false
getFileAndLine(): string

Get file and line string

Return Value
string
getMessage(): string

Get error message

Return Value
string
getMessageHtml(): string

Get the message html-escaped

Return Value
string
html
getMessageText(): string

Get the plain text error message (html tags removed)

Return Value
string
getTrace(bool|auto $withContext = auto): array|false|null

Get backtrace

Description

Backtrace is avail for fatal errors (incl uncaught exceptions) (does not include parse errors)

Parameters
withContext
(auto) Whether to include code snippets
Return Value
array|false|null
log(): bool

Send error to error_log()

Return Value
bool
offsetGet(TKey $key): TValue|null

ArrayAccess getValue.

Parameters
key
Array key
Return Value
TValue|null
Inherited from bdk\ErrorHandler\AbstractError
isFatal(): bool

Is this error "fatal"?

Return Value
bool
setValues(array<TKey,TValue> $values = array()): $this

Clears existing values and sets new values

Parameters
values
key=>value array of values
Return Value
$this
static typeStr(int $type): string

Get human-friendly error type

Parameters
type
E_xx constant value
Return Value
string
Inherited from bdk\PubSub\Event
__debugInfo(): array{ propagationStopped: bool, subject: class-string|mixed, values: array<TKey, TValue>, }

Magic Method

Return Value
array{ propagationStopped: bool, subject: class-string|mixed, values: array<TKey, TValue>, }
getSubject(): ErrorHandler

Get Error's subject (ErrorHandler instance)

Return Value
ErrorHandler
ErrorHandler instance
isPropagationStopped(): bool

Has propagation been stopped?

Description

If stopped, no further event subscribers will be called

Return Value
bool
Whether propagation is stopped for this event
phpDoc
see: Event::stopPropagation()
stopPropagation()

Stops the propagation of the error event

Description

No further event subscribers will be called

Inherited from bdk\PubSub\ValueStore
__serialize(): array<TKey,TValue>

Serialize magic method (since php 7.4)

Return Value
array<TKey,TValue>
__unserialize(array<TKey,TValue> $data)

Unserialize

Parameters
data
serialized data
getIterator(): ArrayIterator<TKey,TValue>

IteratorAggregate interface

Description

Iterate over the object like an array.

Return Value
ArrayIterator<TKey,TValue>
getValue(TKey $key): TValue|null

Get value by key.

Parameters
key
Value name
Return Value
TValue|null
getValues(): array<TKey,TValue>

Get all stored values

Return Value
array<TKey,TValue>
hasValue(TKey $key): bool

Does specified key have a value?

Parameters
key
Value name
Return Value
bool
jsonSerialize(): array<TKey,TValue>

Implements JsonSerializable

Return Value
array<TKey,TValue>
offsetExists(TKey $key): bool

ArrayAccess hasValue.

Parameters
key
Array key
Return Value
bool
offsetSet(TKey $offset, TValue $value)

ArrayAccess setValue

Parameters
offset
Array key to set
value
Value
offsetUnset(TKey $key)

ArrayAccess interface

Parameters
key
Array key
serialize(): string

Implements Serializable

Return Value
string
setValue(TKey $key, TValue $value): $this

Set error value

Parameters
key
Value name
value
Value
Return Value
$this
unserialize(string $data)

Implements Serializable

Parameters
data
serialized data