PHP code example of compono-kit / error-handlers

1. Go to this page and download the library: Download compono-kit/error-handlers 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/ */

    

compono-kit / error-handlers example snippets


use ComponoKit\ErrorHandlers\Interfaces\HandlesErrors;
use ComponoKit\ErrorHandlers\Models\Types\ErrorLevel;

class FileErrorHandler implements HandlesErrors
{
    public function install(): void
    {
        // register global exception / error handlers here
    }

    public function exception(\Throwable $exception, string $message = '', array $context = [], array $tags = []): void
    {
        file_put_contents('error.log', $exception->getMessage() . PHP_EOL, FILE_APPEND);
    }

    public function warning(string $message, array $context = [], array $tags = []): void { /* ... */ }
    public function error(string $message, array $context = [], array $tags = []): void { /* ... */ }
    public function critical(string $message, array $context = [], array $tags = []): void { /* ... */ }
    public function alert(string $message, array $context = [], array $tags = []): void { /* ... */ }
    public function emergency(string $message, array $context = [], array $tags = []): void { /* ... */ }
}

use ComponoKit\ErrorHandlers\ErrorHandlerDelegator;
use ComponoKit\ErrorHandlers\Models\Types\ErrorLevel;

$delegator = new ErrorHandlerDelegator();

// FileErrorHandler receives everything from WARNING upwards
$delegator->addErrorHandler(new FileErrorHandler(), ErrorLevel::WARNING);

// SmsAlertHandler only receives EMERGENCY events
$delegator->addErrorHandler(new SmsAlertHandler(), ErrorLevel::EMERGENCY);

// Activates all registered handlers (e.g. registers global exception handlers)
$delegator->install();

// Only FileErrorHandler is called (WARNING ≥ WARNING, EMERGENCY > WARNING)
$delegator->warning('Disk space below 10 %', ['free_mb' => '80']);

// Both handlers are called (WARNING ≥ WARNING, EMERGENCY ≥ EMERGENCY)
$delegator->emergency('Database unreachable');

// correct
$delegator->error('Payment failed', ['order_id' => '42', 'currency' => 'EUR'], ['checkout']);

// incorrect — nested array and object will be rejected by handlers
$delegator->error('Payment failed', ['order' => $orderObject, 'meta' => ['a' => 'b']]);

// Only reaches handlers registered at WARNING or ERROR — not the SMS alert
$delegator->exception($exception, 'Validation failed');

// Reaches all handlers (default behaviour)
$delegator->exception($exception, 'Payment provider unreachable');

use ComponoKit\ErrorHandlers\Interfaces\ErrorHandlerAware;
use ComponoKit\ErrorHandlers\Traits\ProvidingErrorHandler;

class OrderService implements ErrorHandlerAware
{
    use ProvidingErrorHandler;

    public function placeOrder(string $orderId): void
    {
        try {
            // ...
        } catch (\Throwable $exception) {
            $this->getErrorHandler()->exception($exception, 'Order failed', ['order_id' => $orderId]);
        }
    }
}

$service = new OrderService();
$service->placeOrder('order-42'); // exceptions are swallowed by NullErrorHandler

$service->useErrorHandler($delegator);
$service->placeOrder('order-42'); // exceptions now reach all registered handlers