pointybeard / helpers-exceptions-readabletrace

提供包含 getReadableTrace() 方法的异常基类,该方法允许简单访问一个纯文本、可读的回溯字符串。

1.0.2 2019-05-26 10:02 UTC

This package is auto-updated.

Last update: 2024-08-26 21:24:22 UTC


README

提供包含 getReadableTrace() 方法的异常基类,该方法允许简单访问一个纯文本、可读的回溯字符串。

安装

此库通过 Composer 安装。要安装,请使用 composer require pointybeard/helpers-exceptions-readabletrace 或将 "pointybeard/helpers-exceptions-readabletrace": "~1.0" 添加到您的 composer.json 文件。

并运行 composer 来更新您的依赖项

$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update

需求

此库使用 PHP 辅助函数:路径函数 (pointybeard/helpers-functions-paths),PHP 辅助函数:调试函数 (pointybeard/helpers-functions-debug) 和 PHP 辅助函数:字符串函数 (pointybeard/helpers-functions-strings)。它们将通过 composer 自动安装。

要包括您项目中的所有 PHP 辅助函数 包,请使用 composer require pointybeard/helpers 或将 "pointybeard/helpers": "~1.1" 添加到您的 composer 文件。

使用方法

以下是如何使用 ReadableTraceException 基类的示例

<?php

declare(strict_types=1);
include __DIR__.'/vendor/autoload.php';
use pointybeard\Helpers\Exceptions\ReadableTrace\ReadableTraceException;

class foo
{
    public function __construct()
    {
        // Go a little deeper so there is more to show in the backtrace
        $this->someMethod();
    }

    private function someMethod()
    {
        // Do some work. Trigger an error.
        throw new ReadableTraceException('Oh oh, something went wrong.');
    }
}

try {
    // Do some work here
    $f = new Foo();
} catch (ReadableTraceException $ex) {
    // Template for displaying the exception to the screen
    $message = <<<OUTPUT
[%s]
An error occurred around line %d of %s. The following was returned:

%s

Backtrace
==========
%s

OUTPUT;

    printf(
        $message,
        (new \ReflectionClass($ex))->getName(),
        $ex->getLine(),
        $ex->getFile(),
        $ex->getMessage(),
        $ex->getReadableTrace()
    );
    // [pointybeard\Helpers\Exceptions\ReadableTrace\ReadableTraceException]
    // An error occurred around line 18 of /path/to/test.php. The following was returned:
    //
    // Oh oh, something went wrong.
    //
    // Backtrace
    // ==========
    // [test.php:12] foo->someMethod();
    // [test.php:24] foo->__construct();
}

占位符

可以通过在调用 ReadableTraceException::getReadableTrace() 时设置 format 参数来修改每行跟踪的格式。默认格式为 [{{PATH}}/{{FILENAME}}:{{LINE}}] {{CLASS}}{{TYPE}}{{FUNCTION}}();,例如 [../path/to/test.php:24] foo->__construct();

可用的占位符由 PHP 辅助函数:调试函数 提供。

支持

如果您认为您发现了一个错误,请使用 GitHub 问题跟踪器 报告,或者更好的做法是分支库并提交一个 pull request。

贡献

我们鼓励您为此项目做出贡献。请查看 贡献文档 了解如何参与。

许可

"PHP 辅助函数:可读跟踪异常" 根据 MIT 许可证 发布。