Symfony ErrorHandler Component

repository·8.2·Indexed 25 days ago

https://github.com/symfony/error-handler

A PHP component providing tools to manage errors and debugging. Features include error catching that bypasses the silence operator via ErrorHandler::call(), a debug class loader, custom HTML error templates via HtmlErrorRenderer, and the error:dump command for generating static error pages.

Tokens
704
Snippets
4
Records
5
Agent score
33%

What's inside symfony/error-handler

  1. Enable error debugging

    8.2

    You can enable full debugging features using Debug::enable(). Alternatively, you can enable specific features individually:

    • ErrorHandler::register(): Registers the error handler.
    • DebugClassLoader::enable(): Enables the debug class loader.
    use Symfony\Component\ErrorHandler\Debug;
    use Symfony\Component\ErrorHandler\ErrorHandler;
    use Symfony\Component\ErrorHandler\DebugClassLoader;
    
    Debug::enable();
    
    // or enable only one feature
    //ErrorHandler::register();
    //DebugClassLoader::enable();
  2. Execute code with error catching using ErrorHandler::call()

    8.2

    Use ErrorHandler::call() to wrap a block of code (passed as an anonymous function). If any code inside the function fails, a PHP exception will be thrown, even if the code uses the @ PHP silence operator.

    use Symfony\Component\ErrorHandler\ErrorHandler;
    
    $data = ErrorHandler::call(static function () use ($filename, $datetimeFormat) {
        // if any code executed inside this anonymous function fails, a PHP exception
        // will be thrown, even if the code uses the '@' PHP silence operator
        $data = json_decode(file_get_contents($filename), true);
        $data['read_at'] = date($datetimeFormat);
        file_put_contents($filename, json_encode($data));
    
        return $data;
    });
  3. Set a custom error template

    8.2

    If debug mode is not enabled, you can specify a custom generic HTML template for error rendering using HtmlErrorRenderer::setTemplate().

    // If you want a custom generic template when debug is not enabled
    // HtmlErrorRenderer::setTemplate('/path/to/custom/error.html.php');