Errors
Overview:
Errors handles PHP errors and exceptions and can output in several modes.
Use Errors to capture PHP errors/exceptions and route them to structured handlers or JSON/HTML output depending on runtime context.
Key behavior:
Errors::capture()installs handlers.- Errors trigger both class-level and global events; if no handlers respond, the error is printed.
Public API:
Errors::capture($tracing_level = null)installs handlers.Errors::mode($mode = null)gets or sets output mode.Errors::traceError(...)andErrors::traceException(...)implement handlers.Errors::structuredException(Throwable $e)builds a structured error payload.- Event hooks are exposed through
Errors::on('fatal'|'warning'|'notice'|'any', $listener).
Modes:
Errors::SIMPLE,Errors::HTML,Errors::SILENT,Errors::JSON,Errors::JSON_VERBOSE.
Example:
Errors::mode(Errors::JSON);
Errors::capture(E_ALL);
The Errors module allow you catch and manage errors.
Starting the error handler
---
You can start the error handler via the capture method.
Errors::capture();
From now on errors are converted to ErrorExceptions and routed to the handler which dispatches them in core.error.* events, filtered by kind.
You can bind directly to these events via Errors::on(...).
Event::on('core.error.warning',function($exception){
syslog(LOG_WARNING,$exception->getMessage());
});
Preferred shortcut:
Errors::on('warning', function($exception){
syslog(LOG_WARNING,$exception->getMessage());
});
These are the error mapping rules:
| ErrorType | Gravity | Event | Method |
|---|---|---|---|
E_NOTICE | Informational | core.error.notice | Errors::on('notice', $listener) |
E_USER_NOTICE | Informational | core.error.notice | Errors::on('notice', $listener) |
E_STRICT | Informational | core.error.notice | Errors::on('notice', $listener) |
E_WARNING | Warning | core.error.warning | Errors::on('warning', $listener) |
E_USER_WARNING | Warning | core.error.warning | Errors::on('warning', $listener) |
E_USER_ERROR | Fatal | core.error.fatal | Errors::on('fatal', $listener) |
Every error will be also dispatched via the core.error event. You can bind directly with Errors::on('any', $listener).
If a single error handler returns
true, the current error will be silenced and not propagated any more.
Setting the display mode
---
Errors can be displayed with various formats:
| Modes | Description | Example |
|---|---|---|
Errors::SIMPLE | Prints the error message in plain text | Notice: undefined variable x. |
Errors::HTML | Prints the error message wrapped in html | <pre class="app error"><code>Notice: undefined variable x.</code></pre> |
Errors::SILENT | Don't print anything | |
Errors::JSON | Print a JSON string of an error envelope | {"error":"Notice: undefined variable x."} |
Errors::JSON_VERBOSE | Print a structured JSON envelope with file/line/trace | {"error":"...","type":"...","code":0,"file":"...","line":42,"trace":[...]} |
Errors::mode(Errors::HTML);
JSON_VERBOSE contract
---
Errors::mode(Errors::JSON_VERBOSE) emits a stable structured payload with these required keys:
error(string)type(string)code(int)file(string)line(int)trace(array)
Optional:
previous(nested structured error) when the exception has a previous throwable.