• /
  • Log in
  • Free account

newrelic_notice_error (PHP agent API)

Syntax

newrelic_notice_error(string $message)
newrelic_notice_error(Throwable|Exception $e)
newrelic_notice_error(int $errno, string $errstr, string $errfile, int $errline, string $errcontext)

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 or 5 parameters, depending on your use case.

newrelic_notice_error(string $message)

Parameter

Description

$message

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

$e

exception

Required. Defaults to null.

  • The agent uses Exception or Throwable to capture the stack frame and set the error class to the exception class name.
  • If null or omitted, the agent reports an "exception" in the same format created by Exception::__toString.
newrelic_notice_error(int $errno, string $errstr, string $errfile, int $errline, string $errcontext)

Parameter

Description

$errno

integer

Required. The predefined level of the error, expressed as an integer.

$errstr

string

Required. Provide an error message that will be meaningful to you when it displays in error traces.

$errfile

string

Optional. The name of the file that the error occurred in.

$errline

integer

Optional. The line number where the error occurred.

$errcontext

string

Optional. An array that points to the symbol table that was active when the error occurred.

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.
}

For more help

If you need more help, check out these support and learning resources:

Create issueEdit page
Copyright © 2021 New Relic Inc.