From 7d8b17412e9771e1e3062465bfcd84d8b7470429 Mon Sep 17 00:00:00 2001 From: Amber Sistla Date: Thu, 11 Apr 2024 14:42:04 -0700 Subject: [PATCH] fix(agent): Fix error reporting when EH_THROW is enabled /* * For a the following error codes: * E_WARNING || E_CORE_WARNING || E_COMPILE_WARNING || E_USER_WARNING * PHP triggers an exception if EH_THROW is toggled on and then immediately * returns after throwing the exception. See for more info: * https://github.com/php/php-src/blob/master/main/main.c In that case, we * should not handle it, but we should exist and let the exception handler * deal with it; otherwise, we could record an error even if an exception is * caught. */ --- agent/php_error.c | 20 ++- .../test_EH_THROW_errors_caught_exception.php | 76 +++++++++++ ...est_EH_THROW_errors_uncaught_exception.php | 119 ++++++++++++++++++ 3 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 tests/integration/errors/test_EH_THROW_errors_caught_exception.php create mode 100644 tests/integration/errors/test_EH_THROW_errors_uncaught_exception.php diff --git a/agent/php_error.c b/agent/php_error.c index 59956d585..b4f6f68b1 100644 --- a/agent/php_error.c +++ b/agent/php_error.c @@ -561,12 +561,29 @@ static int nr_php_should_record_error(int type, const char* format TSRMLS_DC) { return 0; } + /* + * For a the following error codes: + * E_WARNING || E_CORE_WARNING || E_COMPILE_WARNING || E_USER_WARNING + * PHP triggers an exception if EH_THROW is toggled on and then immediately + * returns after throwing the exception. See for more info: + * https://github.com/php/php-src/blob/master/main/main.c In that case, we + * should not handle it, but we should exist and let the exception handler + * deal with it; otherwise, we could record an error even if an exception is + * caught. + */ + if (EG(error_handling) == EH_THROW) { + if ((E_WARNING == type) || (E_CORE_WARNING == type) + || (E_COMPILE_WARNING == type) || (E_USER_WARNING == type)) { + return 0; + } + } + errprio = nr_php_error_get_priority(type); if (0 == errprio) { return 0; } - + if (NR_SUCCESS != nr_txn_record_error_worthy(NRPRG(txn), errprio)) { return 0; } @@ -625,7 +642,6 @@ void nr_php_error_cb(int type, stack_json = nr_php_backtrace_to_json(0 TSRMLS_CC); errclass = nr_php_error_get_type_string(type); - nr_txn_record_error(NRPRG(txn), nr_php_error_get_priority(type), true, msg, errclass, stack_json); diff --git a/tests/integration/errors/test_EH_THROW_errors_caught_exception.php b/tests/integration/errors/test_EH_THROW_errors_caught_exception.php new file mode 100644 index 000000000..01a779379 --- /dev/null +++ b/tests/integration/errors/test_EH_THROW_errors_caught_exception.php @@ -0,0 +1,76 @@ +getMessage()); +} diff --git a/tests/integration/errors/test_EH_THROW_errors_uncaught_exception.php b/tests/integration/errors/test_EH_THROW_errors_uncaught_exception.php new file mode 100644 index 000000000..79fe89087 --- /dev/null +++ b/tests/integration/errors/test_EH_THROW_errors_uncaught_exception.php @@ -0,0 +1,119 @@ +