Skip to content

Exception handling

By default, the following exceptions are caught and handled

  • \Exception
  • \Error
  • Koded\Http\HTTPError

The error response payload follows the RFC-7807 specification.

If you wish to capture specific exception types and/or return custom error messages, create your own class and register it with the method App::withErrorHandler() to convert the exceptions into the desired HTTP responses.

handle() method signature

handle(request, response, exception): void

If app does not use 3rd party PSR-7 library, then Koded will use its own implementations from the Koded HTTP library.

<?php

# CustomExceptionHandler.php

use Koded\Framework\App;
use Koded\Http\HTTPError;
use Koded\Http\Interfaces\{Request, Response};

class CustomExceptionHandler {

    public static function handle(
        Request $request,
        Response $response,
        HTTPError $exception): void
    {
        // do something with $exception
        // i.e. re-format the error message and set it in the response
    }
}

Register handler

Register before routes() method

Order matters. It is required to register the custom exception handlers BEFORE the routes, otherwise they won't be handled if the request/route has been resolved.

<?php

# index.php

((new App)
    ->withErrorHandler(CustomExceptionHandler::class)
    ->route(/* ... */)
)();

This implementation is using a PSR-7 compatible library and a standard PHP Throwable class.

<?php

use Psr\Http\Message\{ServerRequestInterface, ResponseInterface};

class PDOExceptionHandler {

    public static function handle(
        ServerRequestInterface $request,
        ResponseInterface $response,
        \Throwable $exception): void
    {
        if ($exception instanceof \PDOException) {
            // do something about it
        }
    }
}

Unregister handler

Use App::withoutErrorHandler() method to remove the error handler.

Unregister after custom handlers

Order matters. Unregister the exception handlers AFTER the custom registration, otherwise they won't be removed.

<?php

# index.php

((new App)
    ->withErrorHandler(SomeCustomErrorHandler::class)

    ->withoutErrorHandler(AnotherCustomExceptionHandler::class)
    ->route(/* ... */)
)();