Universal exception and HTTP error handling package for PHP applications. Developed for the Codemonster ecosystem (Annabel framework).

Installs: 26

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/codemonster-ru/errors

v1.1.0 2025-11-15 18:26 UTC

This package is auto-updated.

Last update: 2025-11-15 18:29:02 UTC


README

Latest Version on Packagist Total Downloads License Tests

Universal package for handling exceptions and HTTP errors.

๐Ÿ“ฆ Installation

composer require codemonster-ru/errors

๐Ÿš€ Quick Start

It can be used as part of a framework or on its own in any PHP project.

Example 1. Minimal use

use Codemonster\Errors\Contracts\ExceptionHandlerInterface;
use Codemonster\Errors\Handlers\SmartExceptionHandler;
use Codemonster\Http\Response;

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

$handler = new SmartExceptionHandler();

set_exception_handler(function (Throwable $e) use ($handler) {
    $response = $handler->handle($e);

    if (php_sapi_name() !== 'cli') {
        http_response_code($response->getStatusCode());

        echo (string) $response;
    } else {
        fwrite(STDERR, (string) $response . PHP_EOL);
    }
});

throw new RuntimeException('Something went wrong!');

When you run it, you'll get a neat HTML page (or a text fallback in the CLI), with error information and the correct HTTP code.

Example 2. Integration with a View renderer (e.g. from a framework)

use Codemonster\Errors\Handlers\SmartExceptionHandler;
use Codemonster\View\View;

$view = new View(...);
$viewRenderer = fn(string $template, array $data) => $view->render($template, $data);
$handler = new SmartExceptionHandler($viewRenderer, debug: true);

try {
    throw new RuntimeException('Demo error');
} catch (Throwable $e) {
    $response = $handler->handle($e);

    echo $response;
}

๐Ÿงฑ Template structure

resources/views/errors/
โ”œโ”€โ”€ generic.php # error page for production
โ””โ”€โ”€ debug.php # debug page for developers

๐Ÿงช Testing

You can run tests with the command:

composer test

๐Ÿ‘จโ€๐Ÿ’ป Author

Kirill Kolesnikov

๐Ÿ“œ License

MIT