Skip to content

test(integration): Add additional custom error handler tests #869

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

Merged
merged 8 commits into from
Apr 5, 2024
96 changes: 96 additions & 0 deletions tests/integration/errors/test_error_handler_false.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
The agent should capture and report errors along with a stack trace
when a custom error handler exists but it returns false for a handled
error.
*/

/*SKIPIF
<?php
if (version_compare(PHP_VERSION, "7.0", "<")) {
die("skip: PHP < 7.0.0 not supported\n");
}
*/

/*INI
display_errors=1
log_errors=0
*/

/*EXPECT_REGEX
^\s*(PHP )?Deprecated:\s*Let this serve as a deprecation*
*/

/*EXPECT_TRACED_ERRORS
[
"?? agent run id",
[
[
"?? when",
"OtherTransaction/php__FILE__",
"Let this serve as a deprecation",
"E_USER_DEPRECATED",
{
"stack_trace": "??",
"agentAttributes": "??",
"intrinsics": "??"
},
"?? transaction ID"
]
]
]
*/

/*EXPECT_ERROR_EVENTS
[
"?? agent run id",
{
"reservoir_size": "??",
"events_seen": 1
},
[
[
{
"type": "TransactionError",
"timestamp": "??",
"error.class": "E_USER_DEPRECATED",
"error.message": "Let this serve as a deprecation",
"transactionName": "OtherTransaction\/php__FILE__",
"duration": "??",
"nr.transactionGuid": "??",
"guid": "??",
"sampled": true,
"priority": "??",
"traceId": "??",
"spanId": "??"
},
{},
{}
]
]
]
*/

function errorHandlerOne($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_DEPRECATED:
return false;
}
return false;
}

// set to the user defined error handler
$old_error_handler = set_error_handler("errorHandlerOne");


trigger_error("Let this serve as a deprecation", E_USER_DEPRECATED);

echo("Hello from happy path!");


48 changes: 48 additions & 0 deletions tests/integration/errors/test_error_handler_implicit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
The agent should NOT capture and report error types
when a custom error handler exists but it returns true for that error type.
*/

/*SKIPIF
<?php
if (version_compare(PHP_VERSION, "7.0", "<")) {
die("skip: PHP < 7.0.0 not supported\n");
}
*/

/*INI
display_errors=1
log_errors=0
*/

/*EXPECT_REGEX
Hello from happy path!
*/

/*EXPECT_TRACED_ERRORS null */

/*EXPECT_ERROR_EVENTS null */

function errorHandlerOne($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_DEPRECATED:
return;
}
return false;
}

// set to the user defined error handler
$old_error_handler = set_error_handler("errorHandlerOne");

trigger_error("Let this serve as a deprecation", E_USER_DEPRECATED);

echo("Hello from happy path!");


48 changes: 48 additions & 0 deletions tests/integration/errors/test_error_handler_true.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
The agent should NOT capture and report error types
when a custom error handler exists but it returns true for those error types.
*/

/*SKIPIF
<?php
if (version_compare(PHP_VERSION, "7.0", "<")) {
die("skip: PHP < 7.0.0 not supported\n");
}
*/

/*INI
display_errors=1
log_errors=0
*/

/*EXPECT_REGEX
Hello from happy path!
*/

/*EXPECT_TRACED_ERRORS null */

/*EXPECT_ERROR_EVENTS null */

function errorHandlerOne($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_DEPRECATED:
return true;
}
return false;
}

// set to the user defined error handler
$old_error_handler = set_error_handler("errorHandlerOne");

trigger_error("Let this serve as a deprecation", E_USER_DEPRECATED);

echo("Hello from happy path!");


100 changes: 100 additions & 0 deletions tests/integration/errors/test_error_handler_with_fatal_error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
The agent should capture and report E_COMPILE_ERROR even if a custom error handler
exists and attempts to handle E_COMPILE_ERROR
However, the following fatal error types
cannot be handled with a user defined function:
E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
It will therefore ignore the custom error handler.
*/

/*SKIPIF
<?php
if (version_compare(PHP_VERSION, "8.0", "<")) {
die("skip: PHP < 8.0.0 not supported\n");
}
*/

/*INI
display_errors=1
log_errors=0
*/

/*EXPECT_REGEX
^\s*(PHP )?Warning:\s*Private methods cannot be final as they are never overridden by other classes
*/

/*EXPECT_TRACED_ERRORS
[
"?? agent run id",
[
[
"?? when",
"OtherTransaction/php__FILE__",
"Private methods cannot be final as they are never overridden by other classes",
"E_COMPILE_WARNING",
{
"stack_trace": [],
"agentAttributes": "??",
"intrinsics": "??"
},
"?? transaction ID"
]
]
]
*/

/*EXPECT_ERROR_EVENTS
[
"?? agent run id",
{
"reservoir_size": "??",
"events_seen": 1
},
[
[
{
"type": "TransactionError",
"timestamp": "??",
"error.class": "E_COMPILE_WARNING",
"error.message": "Private methods cannot be final as they are never overridden by other classes",
"transactionName": "OtherTransaction\/php__FILE__",
"duration": "??",
"nr.transactionGuid": "??",
"guid": "??",
"sampled": true,
"priority": "??",
"traceId": "??",
"spanId": "??"
},
{},
{}
]
]
]
*/

function errorHandlerOne($errno, $errstr, $errfile, $errline)
{
echo("Nothing to see here ever apparently.");
switch ($errno) {
case E_COMPILE_WARNING:
return true;
}
return false;
}

// set to the user defined error handler
$old_error_handler = set_error_handler("errorHandlerOne");

class Foo {
final private static function compileWarning(){
echo 'Compile warning',"\n";
}
}

Loading