对我的框架实施错误处理

发布于 2024-09-24 13:44:18 字数 2397 浏览 2 评论 0原文

我即将开始在我的框架中实现错误处理,并寻求一些有关如何构建它的建议。

首先让我解释一下我的框架目前是如何构建的:

我将框架启动与应用程序启动分开,因此应用程序启动中引起的任何错误都应该由专用于此的类专门处理。

我的想法是创建一个名为 Core_Error_exception 的类,将错误报告设置为 E_ALL,因为我的框架对 PHP 5.3 的错误非常严格,然后,当我的应用程序加载时,我将在该类中运行关闭函数来恢复所有更改的默认值。

我想要做的是捕获 E_*_NOTICE 而不是 E_*_ERROR 的所有错误,然后在应用程序启动之前,我告诉类停止捕获错误,因为 < code>Application_Error_Exception 将监视错误。

因此,我需要一种方法来跟踪所有错误,包括异常和触发器,然后在应用程序初始化之前显示框架调试页面。

我正在寻找的类是这样的:

class Core_Error_Exception
{
    var $previus_error_level,$captured_contents;

    private $stack_trace = array();

    public function Core_Error_Exception()
    {
        $this->previus_error_level = error_reporting(-1);
        set_error_handler(array($this,'_collect_error'));
        set_exception_handler(array($this,'_collect_error'));
        ob_start(array($this,'_capture'));
    }

    public function _collect_error($errno, $errstr, $errfile, $errline, $context)
    {
        $this->stack_trace[] = array(
            array('name' => 'Error ID:',    'value' => $errno),
            array('name' => 'Error String:','value' => $errstr),
            array('name' => 'Error File:',  'value' => $errfile),
            array('name' => 'Error Line:',  'value' => $errline),
            array('name' => 'Context PRE:', 'value' => $context)
        );
        var_dump($this->stack_trace);
    }

    /*
     * _capture is used to capture pre_bufferd content.
     */
    public function _capture($content,$bitfeild)
    {
        if($bitfeild & PHP_OUTPUT_HANDLER_START)
        {
            $this->captured_contents = $content;
        }

        if($bitfeild & PHP_OUTPUT_HANDLER_CONT)
        {
            $this->captured_contents .= $content;
        }

        if($bitfeild & PHP_OUTPUT_HANDLER_END)
        {
            $this->captured_contents .= $content;
        }
        return false;
    }
}

所以我想做的是能够以防跌落的方式构造这个类,以便任何可能已触发的通知错误都将被放入一个数组中,如果调用 E_ERROR 通知后,系统会在此时自动关闭,以防止导致更多错误。

我将使用一个小型 html 模板处理程序,在其中可以将错误集传递到上下文中,因此请注意错误和单个 E_*_ERROR(如果适用)。

构建此类的最佳方法是什么,因为过去我在进行错误跟踪/报告时遇到了一些困难。

更新:使用当前类

如果触发错误,例如trigger_error('test',XXX);我希望能够跟踪所有错误,直到应用程序启动或触发 E_USER_ERROR 为止。

有时我很难完全掌握 PHP 的错误系统,等等,因为有时我对如何构建它以防万一感到困惑。

I am about to start implementing error handling into my framework and looking for some advice on how to build it.

Firstly let me explain how my framework is currently built up:

Im separating the framework startup from the application startup, so any errors caused within the application startup should be handled specifically by a class dedicated to that.

My idea is to have a class called Core_Error_exception witch will set the error reporting to E_ALL as my framework will be strict on errors for PHP 5.3, then as my as my application load I will run a shutdown function function within that class to restore all default values changed.

What im looking to do is capture all errors that are E_*_NOTICE and not E_*_ERROR and then before the application starts I tell the class to stop capturing errors as the Application_Error_Exception will be watching out for errors.

So i will need a way to track all errors including exceptions and triggers and then before the application initializes show a framework debug page.

The kind of class I was looking for is like so:

class Core_Error_Exception
{
    var $previus_error_level,$captured_contents;

    private $stack_trace = array();

    public function Core_Error_Exception()
    {
        $this->previus_error_level = error_reporting(-1);
        set_error_handler(array($this,'_collect_error'));
        set_exception_handler(array($this,'_collect_error'));
        ob_start(array($this,'_capture'));
    }

    public function _collect_error($errno, $errstr, $errfile, $errline, $context)
    {
        $this->stack_trace[] = array(
            array('name' => 'Error ID:',    'value' => $errno),
            array('name' => 'Error String:','value' => $errstr),
            array('name' => 'Error File:',  'value' => $errfile),
            array('name' => 'Error Line:',  'value' => $errline),
            array('name' => 'Context PRE:', 'value' => $context)
        );
        var_dump($this->stack_trace);
    }

    /*
     * _capture is used to capture pre_bufferd content.
     */
    public function _capture($content,$bitfeild)
    {
        if($bitfeild & PHP_OUTPUT_HANDLER_START)
        {
            $this->captured_contents = $content;
        }

        if($bitfeild & PHP_OUTPUT_HANDLER_CONT)
        {
            $this->captured_contents .= $content;
        }

        if($bitfeild & PHP_OUTPUT_HANDLER_END)
        {
            $this->captured_contents .= $content;
        }
        return false;
    }
}

So what im looking to do is to be able to construct this class in a fall-proof way so that any notice errors that may have been triggered will be placed into an array, if an E_ERROR notice is called then this automatically run the shut-down at that point, to prevent more errors being caused.

I will be using a small html template handler where I can pass into that context sets of errors, so Notice errors and a single E_*_ERROR if applicable.

Whats the best way to build this class as in the past I have had some difficulty in doing Error Tracking / Reporting.

Updated: with current class

If errors are triggered such as trigger_error('test',XXX); I want to be able to track all errors until the application launches or an E_USER_ERROR has been triggered.

Sometimes I struggle to fully grasp PHP's error system, and so on as sometimes i get confused in how to build it so its fall-proof.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情定在深秋 2024-10-01 13:44:18

我不太确定你在做什么,但最直接的方法是使用嵌套的 try 块,类似于

in Class Application:

    function run() {
        try {
            --do stuff
        } catch(AppException $e) {
            -- handle application-level exception
        }
        -- all other exceptions fall through


in Class Core:

    try {
        $core->init();
        $application->run(); <-- calls the above function
        $core->done();
    } catch(Exception $e) {
        --last chance exception handler
        --process exceptions the Application was unable to handle on its own
    }

To be able to catch phpbuilt-in error or trigger_error events这样,您还应该始终安装错误到异常处理程序

I'm not quite sure about what you're doing, but the most straightforward way would be to use nested try blocks, along the lines of

in Class Application:

    function run() {
        try {
            --do stuff
        } catch(AppException $e) {
            -- handle application-level exception
        }
        -- all other exceptions fall through


in Class Core:

    try {
        $core->init();
        $application->run(); <-- calls the above function
        $core->done();
    } catch(Exception $e) {
        --last chance exception handler
        --process exceptions the Application was unable to handle on its own
    }

To be able to catch php built-in errors or trigger_error events this way, you also should always install an errors-to-exceptions handler.

月竹挽风 2024-10-01 13:44:18

你所拥有的看起来相当坚固。您必须添加一些逻辑,以便在引发错误并关闭脚本时刷新 ob 缓冲区中的输出,并将所有收集到的数据发送到错误 HTML 文件以显示跟踪。例如:

public function _collect_error($errno, $errstr, $errfile, $errline, $context)
{
    $this->stack_trace[] = array(
        array('name' => 'Error ID:',    'value' => $errno),
        array('name' => 'Error String:','value' => $errstr),
        array('name' => 'Error File:',  'value' => $errfile),
        array('name' => 'Error Line:',  'value' => $errline),
        array('name' => 'Context PRE:', 'value' => $context)
    );

    if($errno == E_USER_ERROR) {
        ob_clean();
        // Pass collected data to HTML template
        // Display HTML
        exit();
    }

    var_dump($this->stack_trace);
}

我不能 100% 确定您可以从致命错误中正常恢复,但从您所说的情况来看,您只是在寻找特定的非致命错误来关闭处理。

另外,您捕获缓冲内容的意图是什么?我假设如果您正在寻找的错误从未被命中,或者它被抛出并显示错误屏幕(为什么我在错误函数中添加了 ob_clean ),那么它就会显示?

What you have looks pretty solid. You would have to add some logic so when the error is thrown that shuts down your script you flush output in the ob buffer and send all the collected data to your error HTML file to display the trace. Something like:

public function _collect_error($errno, $errstr, $errfile, $errline, $context)
{
    $this->stack_trace[] = array(
        array('name' => 'Error ID:',    'value' => $errno),
        array('name' => 'Error String:','value' => $errstr),
        array('name' => 'Error File:',  'value' => $errfile),
        array('name' => 'Error Line:',  'value' => $errline),
        array('name' => 'Context PRE:', 'value' => $context)
    );

    if($errno == E_USER_ERROR) {
        ob_clean();
        // Pass collected data to HTML template
        // Display HTML
        exit();
    }

    var_dump($this->stack_trace);
}

I'm not 100% certain you can gracefully recover from a fatal error, but from what you are saying you are just looking for specific non-fatal errors to shut down processing.

Also what is your intent with capturing buffered content? I would assume it's either displayed if the error your are looking for is never hit or its thrown out and an error screen is shown (why I added ob_clean in the error function)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文