krmgns/errorise

优雅地处理PHP中的错误。

2.0.1 2024-01-26 02:42 UTC

This package is auto-updated.

Last update: 2024-08-26 04:31:47 UTC


README

如果你厌倦了使用@错误抑制运算符,然后用error_get_last()函数查找错误,你可以尝试使用Errorise。Errorise提供捕获的PHP错误,以便你在自己的一侧处理它们,让你从每个错误敏感的调用中解放出来。

安装

composer require krmgns/errorise

使用ErrorHandler

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);
}

为特定函数/模式使用ErrorHandler

你可以控制何时抛出,以及为哪个函数或消息模式抛出。

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 {
    // ...
}

为未定义变量使用ErrorHandler

就像对于函数错误一样,ErrorHandler同样适用于未定义变量的错误。

try {
    $bar = $foo;

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

以非自动模式使用ErrorHandler

如果你想要对注册/注销例程有完全控制,将$auto参数设置为false,就像

$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();
}

在捕获中获取错误信息

你可以通过两种方式使用捕获的ErrorException来获取错误信息。

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

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

利用Error对象

要获取更多信息,你可以利用传递给捕获的ErrorExceptionErrorHandler$error属性。

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 {
    // ...
}

使用ErrorWrapper

你可以使用ErrorWrapper来包装你的调用,而不是使用try/catch块。

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

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

手动处理最后一个错误

你可以在检查你的调用结果后使用LastErrorException来抛出错误。

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()
    );
}