You are reading the documentation for the 1.x version. Switch to the current version 2.x.

Error handler

Last updated: Fri, 29 May 2020 14:19

You can personalize errors pages with an error handler. Also, you can define an error handler by status code.

Error handler

Error handler is a class whose implements \Berlioz\HttpCore\Http\HttpErrorHandler interface.

class MyHttpErrorHandler implements HttpErrorHandler
{
    public function handle(?ServerRequestInterface $request, HttpException $e): ResponseInterface
    {
        // ...
    }
}

If you want use the template rendering engine or access to core functionalities, you need to extend \Berlioz\HttpCore\Controller\AbstractController class.

class MyHttpErrorHandler extends AbstractController implements HttpErrorHandler
{
    public function handle(?ServerRequestInterface $request, HttpException $e): ResponseInterface
    {
        // ...

        return $this->response($this->render('error.html.twig'));
    }
}

Configuration

Your error handler must be declared in your configuration file like this:

{
    "berlioz": {
        "http": {
            "errors": {
                "default": "App\\Http\\MyHttpErrorHandler"
            }
        }
    }
}

The key default, it's for all errors attempted if no other handler is declared. So you can declare others handlers for specific http errors, like 500 errors:

{
    "berlioz": {
        "http": {
            "errors": {
                "default": "App\\Http\\MyHttpErrorHandler",
                "500": "App\\Http\\InternalHttpErrorHandler"
            }
        }
    }
}