newrelic_notice_error

newrelic_notice_error(string $message[, exception $exception])
Report an error at this line of code and collect a complete stack trace.

Requirements

Agent version 2.6 or higher.

Description

Report an error at the line of code where the method is called, with a complete stack trace. Include this method as part of an error handler. When there are multiple calls to this method in a single transaction, the agent only retains the exception from the last call.

Parameters

Parameter Description

$message

string

Required. Provide a meaningful error message for use in error traces.

$exception

exception

Optional. Defaults to null.

If you pass an exception, you must pass a valid PHP Exception class. The agent captures the stack frame from that class, rather than the stack at the time this function was called.

If null or omitted, the agent reports an "exception" in the same format created by Exception::__toString.

Return value(s)

Returns null regardless of result.

Example(s)

Report error messages with line numbers

// Error handler function
function example_handler($errno, $errstr) {
    if (extension_loaded('newrelic')) { // Ensure PHP agent is available
        newrelic_notice_error($errno . $errstr, null);
    }
}

// Set error handler
set_error_handler("example_handler");

Report error message and exception

// Exception handler function
function example_handler (Exception $ex)
{
    if (extension_loaded('newrelic')) { // Ensure PHP agent is available
        newrelic_notice_error($ex->getMessage(), $ex);
    }
    throw $ex;
}

// Set exception handler
set_exception_handler("example_handler");