PHP code example of tobento / service-error-handler

1. Go to this page and download the library: Download tobento/service-error-handler 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/ */

    

tobento / service-error-handler example snippets


use Tobento\Service\ErrorHandler\ErrorHandling;
use Tobento\Service\ErrorHandler\ThrowableHandlers;
use Tobento\Service\ErrorHandler\ThrowableHandlerFactory;
use Tobento\Service\ErrorHandler\Handler;

$throwableHandlers = new ThrowableHandlers(new ThrowableHandlerFactory());

// adding any handler:
// $throwableHandlers->add(ValidationExceptionHandler::class);
// $throwableHandlers->add(GeneralExceptionHandler::class);

// only on development:
$throwableHandlers->add(Handler\Debug::class);

// adding last:
$throwableHandlers->add(Handler\Errors::class);

(new ErrorHandling($throwableHandlers))->register();

use Tobento\Service\ErrorHandler\ErrorHandling;
use Tobento\Service\ErrorHandler\ThrowableHandlers;
use Tobento\Service\ErrorHandler\ThrowableHandlersInterface;
use Tobento\Service\ErrorHandler\ThrowableHandlerFactory;

$throwableHandlers = new ThrowableHandlers(new ThrowableHandlerFactory());

var_dump($throwableHandlers instanceof ThrowableHandlersInterface);
// bool(true)

use Tobento\Service\ErrorHandler\ErrorHandling;
use Tobento\Service\ErrorHandler\ThrowableHandlers;
use Tobento\Service\ErrorHandler\ThrowableHandlersInterface;
use Tobento\Service\ErrorHandler\AutowiringThrowableHandlerFactory;

// Any PSR-11 container
$container = new \Tobento\Service\Container\Container();

$throwableHandlers = new ThrowableHandlers(
    new AutowiringThrowableHandlerFactory($container)
);

var_dump($throwableHandlers instanceof ThrowableHandlersInterface);
// bool(true)

$throwableHandlers->add(new Handler\Errors());

$throwableHandlers->add(Handler\Errors::class);

$throwableHandlers->add([ThrowableHandler::class, 'name' => 'value']);

use Throwable;

$throwableHandlers->add(function(Throwable $t): mixed {
    // Return throwable if cannot handle, otherwise anything else.
    return $t;
});

$throwableHandlers->add(ThrowableHandler::class)
                  ->level(\E_USER_WARNING, \E_WARNING);

$throwableHandlers->add(ThrowableHandler::class)
                  ->handles(SomeException::class, AnotherException::class);

$throwableHandlers->add(ThrowableHandler::class)
                  ->priority(1000); // is default

use Throwable;

try {
    // do something
} catch (Throwable $t) {
    // do something with the response:
    $response = $throwableHandlers->handleThrowable($t);
}

use Tobento\Service\ErrorHandler\Handler\Debug;
use Tobento\Service\View\ViewInterface;
use Tobento\Service\ErrorHandler\ThrowableHandlerInterface;

$debug = new Debug(
    view: null, // null|ViewInterface
);

var_dump($debug instanceof ThrowableHandlerInterface);
// bool(true)

use Tobento\Service\ErrorHandler\Handler\Debug;
use Tobento\Service\View\ViewInterface;
use Tobento\Service\View\View;
use Tobento\Service\View\PhpRenderer;
use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\Dir;

$view = new View(
    new PhpRenderer(
        new Dirs(
            new Dir('/private/view'),
        )
    )
);

$debug = new Debug(
    view: $view, // null|ViewInterface
);

use Tobento\Service\ErrorHandler\Handler\Errors;
use Tobento\Service\View\ViewInterface;
use Tobento\Service\ErrorHandler\ThrowableHandlerInterface;

$errors = new Errors(
    view: null, // null|ViewInterface
);

var_dump($errors instanceof ThrowableHandlerInterface);
// bool(true)

use Tobento\Service\ErrorHandler\Handler\Errors;
use Tobento\Service\View\ViewInterface;
use Tobento\Service\View\View;
use Tobento\Service\View\PhpRenderer;
use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\Dir;

$view = new View(
    new PhpRenderer(
        new Dirs(
            new Dir('/private/view'),
        )
    )
);

$errors = new Errors(
    view: $view, // null|ViewInterface
);

use Tobento\Service\ErrorHandler\Handler\Log;
use Psr\Log\LoggerInterface;
use Monolog\Logger;
use Monolog\Handler\TestHandler;
use Tobento\Service\ErrorHandler\ThrowableHandlerInterface;

$logger = new Logger('name');
$logger->pushHandler(new TestHandler());

$log = new Log(
    logger: $logger, // Closure|LoggerInterface
);

var_dump($log instanceof ThrowableHandlerInterface);
// bool(true)

use Tobento\Service\ErrorHandler\Handler\Log;
use Psr\Log\LoggerInterface;
use Monolog\Logger;
use Monolog\Handler\TestHandler;

$throwableHandlers->add(new Log(function(): LoggerInterface {
    $logger = new Logger('name');
    $testHandler = new TestHandler();
    $logger->pushHandler($testHandler);
    return $logger;
}))->levels(E_ERROR, E_CORE_ERROR);

private/
    view/
        debug/
            error.php
            throwable.php

private/
    view/
        error.php