Skip to content

Commit

Permalink
test(integration): Add additional custom error handler tests.
Browse files Browse the repository at this point in the history
Added additional error tests to exercise a variety of return values from a custom error handler.
  • Loading branch information
zsistla committed Mar 29, 2024
1 parent c219107 commit 12f2dc8
Show file tree
Hide file tree
Showing 6 changed files with 496 additions and 0 deletions.
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("Let this serve as a warning");


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
Let this serve as a warning
*/

/*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("Let this serve as a warning");


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
Let this serve as a warning
*/

/*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("Let this serve as a warning");


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

0 comments on commit 12f2dc8

Please sign in to comment.