PHP code example of krmgns / errorise

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

    

krmgns / errorise example snippets


use Errorise;

$eh = new Errorise\ErrorHandler();
try {
    fopen('/path/to/file.txt', 'r');

    // Throws if any error occured.
    $eh->throw();
} catch (Errorise\ErrorException $e) {
    // Message: fopen(/path/to/file.txt): Failed to open ...
    throw new YourCustomException_After_Some_Business(
        $e->getMessage()
    );
} finally {
    // Trigger handler __destruct() to call unregister().
    unset($eh);
}

try {
    fopen('/path/to/file.txt', 'r');

    // Throws if any error occured with fopen().
    $eh->throwFor('fopen');

    // Throws if any error occured with message pattern.
    $eh->throwForMatch('/fopen/');
} catch (Errorise\ErrorException $e) {
    // ...
} finally {
    // ...
}

try {
    $bar = $foo;

    // Throws since $foo is undefined.
    $eh->throw();
} catch (Errorise\ErrorException $e) {
    // ...
} finally {
    // ...
}

$eh = new Errorise\ErrorHandler(false);
try {
    // Register Errorise error handler.
    $eh->register();

    // Some risky or error-prone works.

    // Throws if any error occured.
    $eh->throw();
} catch (Errorise\ErrorException $e) {
    // ...
} finally {
    // Un-Register Errorise error handler.
    // So, back to the previous or internal error handler.
    $eh->unregister();
}

try {
    // ...
} catch (Errorise\ErrorException $e) {
    // Message: mkdir(): No such file or directory
    $e->getMessage();

    // Message: No such file or directory
    $e->getPureMessage();
} finally {
    // ...
}

try {
    // ...
} catch (Errorise\ErrorException $e) {
    // @var Errorise\Error
    $error = $e->error();

    // Data: [severity, message, file, line]
    $data = $error->data();

    // Severity: 2
    $error->getSeverity();

    // Message: mkdir(): No such file or directory
    $error->getMessage();

    // File: /tmp/php/errorise/test.php
    $error->getFile();

    // Line: 3, where mkdir() was called.
    $error->getLine();

    // Function / Variable Name.
    $error->getFunction();
    $error->getVariable();
} finally {
    // ...
}

$ret = Errorise\ErrorWrapper::wrap(function () {
    $fp = fopen('/path/to/file.txt', 'r');
    return $fp;
}, $e /* byref */);

assert($ret == false);
assert($e instanceof Errorise\ErrorException);

use Errorise;

// Your filesystem module.
class FileSystem {
    public static function createDirectory(
        string $dir, int $mode = 0777, bool $recursive = false
    ): void {
        $ok = @mkdir($dir, $mode, $recursive);
        if (!$ok) {
            throw new Errorise\LastErrorException();
        }
    }
}

// Your client layer.
try {
    FileSystem::createDirectory('/tmp');
} catch (Errorise\LastErrorException $e) {
    // Message: mkdir(): File exists
    throw new YourCustomException_After_Some_Business(
        $e->getMessage()
    );
}