Syntax
newrelic_notice_error(string $message)newrelic_notice_error(Throwable|Exception $e)newrelic_notice_error(string $errstr, Throwable|Exception $e)newrelic_notice_error(int $errno, string $errstr, string $errfile, int $errline) (PHP 8+)newrelic_notice_error(int $errno, string $errstr, string $errfile, int $errline, string $errcontext) (PHP 7)
Use these calls to collect errors that the PHP agent does not collect automatically and to set the callback for your own error and exception handler.
Requirements
Agent version 2.6 or higher.
Caution
If you include an exception ($e
), there are differences depending on the PHP version you are using:
- PHP version 5 or lower: You must pass a valid PHP Exception class.
- PHP version 7 or higher: You must pass a valid PHP Throwable interface.
Description
The PHP agent handles PHP errors and exceptions automatically for supported frameworks.
If you want to collect errors that are not handled automatically so that you can query for those errors in New Relic and view error traces, you can use newrelic_notice_error
.
If you want to use your own error and exception handlers, you can set newrelic_notice_error
as the callback.
Collect errors that are not handled automatically
To collect errors that the PHP agent does not handle automatically, such as non-PHP errors, add this call to the function that you want to report on:
newrelic_notice_error(Throwable|Exception $e)
Important
When there are multiple calls to this function in a single transaction, the PHP agent retains the exception from the last call only.
Set the callback for your own error and exception handler
To use your own handler, use these calls to make sure that the PHP agent notices the errors and exceptions from within your handler.
Parameters
This function can handle a variable number of parameters. You can pass-in 1, 4 or 5 parameters, depending on your use case.
newrelic_notice_error(string $message)
Parameter | Description |
---|---|
string | Required. Provide an error message that will be meaningful to you when it displays in error traces. |
newrelic_notice_error(Throwable|Exception $e)
Parameter | Description |
---|---|
exception | Required. Defaults to
|
PHP 7.3
newrelic_notice_error(int $errno, string $errstr, string $errfile, int $errline, string $errcontext)
PHP 8.x
newrelic_notice_error(int $errno, string $errstr, string $errfile, int $errline)
Parameter | Description |
---|---|
integer | Required. The predefined level of the error, expressed as an integer. |
string | Required. Provide an error message that will be meaningful to you when it displays in error traces. This can be used to include additional information you would like to see. |
string | Optional. The name of the file that the error occurred in. NOTE: This parameter is ignored by the agent - the agent will provide a stack trace with this information. |
integer | Optional. The line number where the error occurred. NOTE: This parameter is ignored by the agent - the agent will provide a stack trace with this information. |
string | Optional. An array that points to the symbol table that was active when the error occurred. NOTE: The agent ignores this parameter - and it is not supported with PHP 8+. |
Return values
Returns null
regardless of result.
Examples
Collect errors that are not handled automatically
Track errors that aren't reported automatically or that aren't PHP errors. In this example, an error is sent to the PHP agent if an unknown user accesses your app.
try { // Add your code that may throw an error here.} catch (UserNotFoundException $e) { newrelic_notice_error($e); // Handle normally.}
Report exceptions from your own exception handler
Make the PHP agent notice exceptions from within your own exception handler.
function example_exception_handler($ex) { if (extension_loaded('newrelic')) { newrelic_notice_error($ex); } //Add your code here.}
Report errors from your own error handler (PHP version 5.6 or higher)
PHP version 5.6 or higher: Make the PHP agent notice errors from within your own error handler.
function example_error_handler($errno, $errstr, $errfile = null, $errline = null, $errcontext = null) { if (extension_loaded('newrelic')) { newrelic_notice_error(...func_get_args()); } //Add your code here.}
Report errors from your own error handler (PHP version 5.5 or lower)
PHP version 5.5 or lower: Make the PHP agent notice errors from within your own error handler.
function example_error_handler($errno, $errstr, $errfile = null, $errline = null, $errcontext = null) { if (extension_loaded('newrelic')) { call_user_func_array('newrelic_notice_error', func_get_args()); } //Add your code here.}