PHP code example of codemonster-ru / errors

1. Go to this page and download the library: Download codemonster-ru/errors library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codemonster-ru / errors example snippets


use Codemonster\Errors\Handlers\SmartExceptionHandler;

_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!');

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;
}

$handler = new SmartExceptionHandler(
    viewRenderer: null,
    debug: false,
    templatePath: __DIR__ . '/resources/views/errors'
);

resources/views/errors/
- generic.php # error page for production
- debug.php # debug page for developers
- 404.php # optional, per-status page
- 500.php # optional, per-status page