Req #65140 [Csd]: Pass context to assert callback functions
| From: | karlnack at fastmail dot us | Date: | Wed, 11 Feb 2015 22:48:50 +0000 |
| Subject: | Req #65140 [Csd]: Pass context to assert callback functions | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-190595@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=65140&edit=1
ID: 65140
User updated by: karlnack at fastmail dot us
Reported by: karlnack at fastmail dot us
Summary: Pass context to assert callback functions
Status: Closed
Type: Feature/Change Request
Package: Unknown/Other Function
Operating System: n/a
PHP Version: Irrelevant
Assigned To: yohgaki
Block user comment: N
Private report: N
New Comment:
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.
}
}
Previous Comments:
------------------------------------------------------------------------
[2015-02-11 20:36:34] yohgaki@php.net
Could you post the way you did?
------------------------------------------------------------------------
[2015-02-11 20:14:02] 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.
------------------------------------------------------------------------
[2013-06-27 03:34:27] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65140&edit=1