Global error handler

QuietGuard\Monitor\ErrorHandler registers PHP's global handlers so a plain-PHP host with no framework exception pipeline still reports uncaught errors. Frameworks (Laravel, Symfony) have their own hooks and should not call this.

Registering

ErrorHandler::register() is a single static call that takes a configured Reporter:

php
use QuietGuard\Monitor\ErrorHandler;
use QuietGuard\Monitor\Reporter;

/** @var Reporter $reporter */
ErrorHandler::register($reporter);

Call it once, as early as possible in your bootstrap, after the Reporter is built.

What it registers

register() wires three global hooks:

  1. set_exception_handler, any uncaught Throwable is sent via $reporter->reportException($e). The handler chains: if another global exception handler was registered before ErrorHandler::register(), it still runs after the exception has been reported (the monitor reports first, then delegates). When no previous handler exists, the exception (message, file:line and stack trace) is written to error_log, matching what PHP's default behavior would have recorded: installing monitoring never removes the host's local crash record.
  2. set_error_handler, a raised PHP error is wrapped in an ErrorException and reported. It first checks error_reporting() & $severity, so values suppressed with @ or below the current error_reporting level are skipped. It then returns false so PHP's normal error handling still runs.
  3. register_shutdown_function, on shutdown, error_get_last() is inspected and, if it is a fatal type (E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR), an ErrorException is reported. This catches fatals that the error handler cannot.

Every path runs through the Reporter, which never throws, so registering these handlers cannot break the host.

A minimal plain-PHP bootstrap

php
use QuietGuard\Monitor\Config;
use QuietGuard\Monitor\ErrorHandler;
use QuietGuard\Monitor\Reporter;
use QuietGuard\Monitor\Http\CurlHttpClient;
use QuietGuard\Monitor\Payload\ExceptionPayloadBuilder;
use QuietGuard\Monitor\Support\Scrubber;

require __DIR__.'/vendor/autoload.php';

$config = new Config(url: 'https://monitor.example.com', key: 'project-token');

$reporter = new Reporter(
    config: $config,
    http: new CurlHttpClient(),
    scrubber: new Scrubber(['password', 'authorization']),
    builder: new ExceptionPayloadBuilder($config->traceLimit, $config->release),
);

ErrorHandler::register($reporter);

// From here on, uncaught exceptions, PHP errors and fatal shutdown errors
// are forwarded to Quiet Guard automatically.
The Laravel SDK, the Symfony bundle and the WordPress plugin do not use ErrorHandler: Laravel and Symfony hook their own exception pipelines, and the WordPress plugin integrates with WordPress. Reach for ErrorHandler only in a host that has no exception pipeline of its own.

You are reading the PHP Core v1.0 documentation.