PHP code example of josantonius / exception-handler

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

    

josantonius / exception-handler example snippets


/**
 * Sets a exception handler.
 *
 * @param callable $callback          Exception handler function.
 * @param string[] $runBeforeCallback Method names to call in the exception before run callback.
 * @param string[] $runAfterCallback  Method names to call in the exception after run callback.
 * 
 * @throws NotCallableException     if the callback is not callable.
 * @throws WrongMethodNameException if the method names are not string or are empty.
 * 
 * @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
 */
public function __construct(
    private callable $callback,
    private array $runBeforeCallback = [],
    private array $runAfterCallback = []
);

use Josantonius\ExceptionHandler\Exceptions\NotCallableException;
use Josantonius\ExceptionHandler\Exceptions\WrongMethodNameException;

use Josantonius\ExceptionHandler\ExceptionHandler;

function handler(\Throwable $exception) { /* do something */ }

new ExceptionHandler(
    callback: handler(...)
);

/**
 * If an exception is thrown, the following is executed:
 *
 * handler($exception)
 */

use Josantonius\ExceptionHandler\ExceptionHandler;

class FooException extends \Exception
{
    public function context(): void { /* do something */ }
}

class Handler {
    public function exceptions(Throwable $exception): void
    {
        if ($exception instanceof FooException) {
            /* do something */
        }
    }
}

new ExceptionHandler(
    callback: (new Handler())->exceptions(...),
    runBeforeCallback: ['context']
);

/**
 * If FooException() is thrown, the following is executed:
 * 
 * FooException->context()
 * Handler->exceptions($exception)
 */

use Josantonius\ExceptionHandler\ExceptionHandler;

class FooException extends \Exception
{
    public function report(): void { /* do something */ }

    public function render(): void { /* do something */ }
}

class Handler {
    public static function exceptions(Throwable $exception): void
    {
        if ($exception instanceof FooException) {
            /* do something */
        }
    }
}

new ExceptionHandler(
    callback: Handler::exceptions(...),
    runAfterCallback: ['report', 'render']
);

/**
 * If FooException() is thrown, the following is executed:
 * 
 * Handler::exceptions($exception)
 * FooException->report()
 * FooException->render()
 */

use Josantonius\ExceptionHandler\ExceptionHandler;

class FooException extends \Exception
{
    public function context(): void { /* do something */ }

    public function report(): void { /* do something */ }

    public function render(): void { /* do something */ }
}

function exceptionHandler(Throwable $exception) { /* do something */ }

new ExceptionHandler(
    callback: exceptionHandler(...),
    runBeforeCallback: ['context', 'logger'],
    runAfterCallback: ['report', 'render']
);

/**
 * If FooException() is thrown, the following is executed:
 * 
 * FooException->context()
 * exceptionHandler($exception)
 * FooException->report()
 * FooException->render()
 * 
 * FooException->logger() is ignored, does not exist in the exception.
 */