Skip to content

add get_error_handler() and get_exception_handler() #969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Zend/tests/error_handler_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
error handler tests - 1
--FILE--
<?php

var_dump(get_error_handler());

var_dump(set_error_handler("my_error_handler"));

trigger_error("test", E_USER_WARNING);

var_dump(restore_error_handler());

var_dump(get_error_handler());

function my_error_handler($errno, $errstr, $errfile, $errline) {
var_dump($errno, $errstr, $errfile, $errline);
}

echo "Done\n";
?>
--EXPECTF--
NULL
NULL
int(512)
string(4) "test"
string(%d) "%s/Zend/tests/error_handler_001.php"
int(7)
bool(true)
NULL
Done

30 changes: 30 additions & 0 deletions Zend/tests/exception_handler_007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
exception handler tests - 1
--FILE--
<?php

var_dump(get_exception_handler());

var_dump(set_exception_handler("my_exception_handler"));

throw new test();

function my_exception_handler($e) {
var_dump(get_class($e)." thrown!");

var_dump(restore_exception_handler());

var_dump(get_exception_handler());
}

class test extends Exception {
}

echo "Done\n";
?>
--EXPECTF--
NULL
NULL
string(12) "test thrown!"
bool(true)
NULL
32 changes: 32 additions & 0 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ static ZEND_FUNCTION(get_object_vars);
static ZEND_FUNCTION(get_class_methods);
static ZEND_FUNCTION(trigger_error);
static ZEND_FUNCTION(set_error_handler);
static ZEND_FUNCTION(get_error_handler);
static ZEND_FUNCTION(restore_error_handler);
static ZEND_FUNCTION(set_exception_handler);
static ZEND_FUNCTION(get_exception_handler);
static ZEND_FUNCTION(restore_exception_handler);
static ZEND_FUNCTION(get_declared_classes);
static ZEND_FUNCTION(get_declared_traits);
Expand Down Expand Up @@ -286,8 +288,10 @@ static const zend_function_entry builtin_functions[] = { /* {{{ */
ZEND_FE(trigger_error, arginfo_trigger_error)
ZEND_FALIAS(user_error, trigger_error, arginfo_trigger_error)
ZEND_FE(set_error_handler, arginfo_set_error_handler)
ZEND_FE(get_error_handler, arginfo_zend__void)
ZEND_FE(restore_error_handler, arginfo_zend__void)
ZEND_FE(set_exception_handler, arginfo_set_exception_handler)
ZEND_FE(get_exception_handler, arginfo_zend__void)
ZEND_FE(restore_exception_handler, arginfo_zend__void)
ZEND_FE(get_declared_classes, arginfo_zend__void)
ZEND_FE(get_declared_traits, arginfo_zend__void)
Expand Down Expand Up @@ -1656,6 +1660,20 @@ ZEND_FUNCTION(set_error_handler)
}
/* }}} */

/* {{{ proto string get_error_handler(void)
Returns the currently defined error handler, or null */
ZEND_FUNCTION(get_error_handler)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and in get_exception_handler(), you should use zend_parse_parameters_none(), else the changes look great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hehe, nice catch, was copying most of the code from restore_error_handler() and surprisingly zend_parse_parameters_none() is not used there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (zend_parse_parameters_none() == FAILURE) {
return;
}

if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
RETVAL_ZVAL(&EG(user_error_handler), 1, 0);
}
}
/* }}} */

/* {{{ proto void restore_error_handler(void)
Restores the previously defined error handler function */
ZEND_FUNCTION(restore_error_handler)
Expand Down Expand Up @@ -1718,6 +1736,20 @@ ZEND_FUNCTION(set_exception_handler)
}
/* }}} */

/* {{{ proto string get_exception_handler(void)
Returns the currently defined exception handler, or null */
ZEND_FUNCTION(get_exception_handler)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}

if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
RETVAL_ZVAL(&EG(user_exception_handler), 1, 0);
}
}
/* }}} */

/* {{{ proto void restore_exception_handler(void)
Restores the previously defined exception handler function */
ZEND_FUNCTION(restore_exception_handler)
Expand Down