classes/Errors.md

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:

Public API:

Modes:

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:

ErrorTypeGravityEventMethod
E_NOTICEInformationalcore.error.noticeErrors::on('notice', $listener)
E_USER_NOTICE Informationalcore.error.noticeErrors::on('notice', $listener)
E_STRICT Informationalcore.error.noticeErrors::on('notice', $listener)
E_WARNING Warningcore.error.warningErrors::on('warning', $listener)
E_USER_WARNING Warningcore.error.warningErrors::on('warning', $listener)
E_USER_ERROR Fatalcore.error.fatalErrors::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:

ModesDescriptionExample
Errors::SIMPLEPrints the error message in plain textNotice: undefined variable x.
Errors::HTMLPrints the error message wrapped in html<pre class="app error"><code>Notice: undefined variable x.</code></pre>
Errors::SILENTDon't print anything
Errors::JSONPrint a JSON string of an error envelope{"error":"Notice: undefined variable x."}
Errors::JSON_VERBOSEPrint 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:

Optional: