php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65140 Pass context to assert callback functions
Submitted: 2013-06-27 03:34 UTC Modified: 2015-02-11 22:48 UTC
From: karlnack at fastmail dot us Assigned: yohgaki (profile)
Status: Closed Package: Unknown/Other Function
PHP Version: Irrelevant OS: n/a
Private report: No CVE-ID: None
 [2013-06-27 03:34 UTC] karlnack at fastmail dot us
Description:
------------
Custom error handlers (registered with set_error_handler()) are passed an $errcontext parameter with an array of every variable that was in scope when the error occurred. This would be similarly useful for assert callback functions registered with assert_options(). This could presumably be passed as a fifth (optional) parameter to the assert callback function.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-11 20:14 UTC] karlnack at fastmail dot us
-Status: Open +Status: Closed
 [2015-02-11 20:14 UTC] karlnack at fastmail dot us
I've figured out how to work around this (by saving the assertion state in the assert handler and letting the error handling continue to the error handler), so I've decided to close this.
 [2015-02-11 20:36 UTC] yohgaki@php.net
-Assigned To: +Assigned To: yohgaki
 [2015-02-11 20:36 UTC] yohgaki@php.net
Could you post the way you did?
 [2015-02-11 22:48 UTC] karlnack at fastmail dot us
Here's a basic example:

class ErrorHandler {    
    private $assertion;

    public static function enable() {        
        // Configure errors.
        error_reporting(E_ALL);        
        
        // Configure assertions.
        assert_options(ASSERT_ACTIVE, 1);
        assert_options(ASSERT_WARNING, 1);
        assert_options(ASSERT_BAIL, 0);
        assert_options(ASSERT_QUIET_EVAL, 0);

        // Register handlers.
        $eh = new ErrorHandler();
        set_error_handler([$eh, 'handle_error'], error_reporting());
        assert_options(ASSERT_CALLBACK, [$eh, 'handle_assertion']);
    }
    
    public function handle_assertion($file, $line, $code, $desc = null) {
        // Save the raw assertion code and description.
        // The actual assertion is handled in the error handler.
        $this->assertion = [$code, $desc];
    }

    public function handle_error($errno, $errstr, $errfile, $errline, $errcontext) {
        if (!(error_reporting() & $errno)) {
            // This error code is not included in error_reporting.
            return;
        }

        if (!$this->assertion) {
            throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
        }

        list($code, $desc) = $this->assertion;
        $this->assertion = null; // Reset the assertion.
        // Handle the assertion here.
    }
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 21:01:30 2024 UTC