PHP code example of mensbeam / catcher

1. Go to this page and download the library: Download mensbeam/catcher 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/ */

    

mensbeam / catcher example snippets


use MensBeam\Catcher;

$catcher = new Catcher();

use MensBeam\Catcher,
    MensBeam\Logger;

$catcher = new Catcher(
    new PlainTextHandler([
        'logger' => new Logger('log'),
        'silent' => true
    ])
);

$catcher = new Catcher();
$catcher->errorHandlingMethod = Catcher::THROW_ALL_ERRORS;

try {
    trigger_error(\E_USER_WARNING, 'Ook!');
} catch (\Throwable $t) {
    echo $t->message();
}

namespace MensBeam;
use MensBeam\Catcher\Handler;

class Catcher {
    public const THROW_NO_ERRORS = 0;
    public const THROW_FATAL_ERRORS = 1;
    public const THROW_ALL_ERRORS = 2;

    public int $errorHandlingMethod = self::THROW_FATAL_ERRORS;
    public bool $preventExit = false;

    public function __construct(Handler ...$handlers);

    public function getHandlers(): array;
    public function getLastThrowable(): ?\Throwable;
    public function isRegistered(): bool;
    public function popHandler(): Handler;
    public function pushHandler(Handler ...$handlers): void;
    public function register(): bool;
    public function setHandlers(Handler ...$handlers): void;
    public function shiftHandler(): Handler;
    public function unregister(): bool;
    public function unshiftHandler(Handler ...$handlers): void;
}

namespace MensBeam\Catcher;

abstract class Handler {
    public const CONTENT_TYPE = null;

    public const NON_FATAL_ERROR = \E_NOTICE | \E_USER_NOTICE | \E_WARNING | \E_COMPILE_WARNING | \E_USER_WARNING | \E_DEPRECATED | \E_USER_DEPRECATED;

    // Control constants
    public const BUBBLES = 1;
    public const EXIT = 2;
    public const LOG = 4;
    public const NOW = 8;
    public const OUTPUT = 16;

    protected ?array $lastOutputThrowable;
    protected array $outputBuffer;

    // Options
    protected int $_backtraceArgFrameLimit = 5;
    protected bool $_bubbles = true;
    protected string $_charset = 'UTF-8';
    protected bool $_forceExit = false;
    protected bool $_forceOutputNow = false;
    protected int $_httpCode = 500;
    protected ?array $_ignore = null;
    protected ?LoggerInterface $_logger = null;
    protected bool $_logWhenSilent = true;
    protected bool $_outputBacktrace = false;
    protected bool $_outputPrevious = true;
    protected bool $_outputTime = true;
    protected bool $_outputToStderr = true;
    protected bool $_silent = false;
    protected string $_timeFormat = 'Y-m-d\TH:i:s.vO';

    public function __construct(array $options = []);

    public function __invoke(): void;
    public function getLastOutputThrowable(): ?array;
    public function getOption(string $name): mixed;
    public function setOption(string $name, mixed $value): void;

    protected function buildOutputArray(ThrowableController $controller): array;
    protected function cleanOutputThrowable(array $outputThrowable): array;
    abstract protected function handleCallback(array $output): array;
    abstract protected function invokeCallback(): void;
    protected function log(\Throwable $throwable, string $message): void;
    protected function print(string $string): void;
    protected function serializeArgs(mixed $value): string;
}

namespace MensBeam\Catcher;

class ThrowableController {
    public function __construct(\Throwable $throwable);

    public function getErrorType(): ?string;
    public function getFrames(int $argFrameLimit = \PHP_INT_MAX): array;
    public function getPrevious(): ?ThrowableController;
    public function getThrowable(): \Throwable;
}

namespace MensBeam\Catcher;

class JSONHandler extends Handler {
    public const CONTENT_TYPE = 'application/json';

    protected bool $_prettyPrint = true;
    protected bool $_prettyPrintWhenLogging = false;
}

namespace MensBeam\Catcher;

class PlainTextHandler extends Handler {
    public const CONTENT_TYPE = 'text/plain';

    protected string $_timeFormat = '[H:i:s]';
}