Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adamdavies's avatar

Custom Exception Handling Not Behaving as Expected

I'm working on an application that uses services and inside the services I have my error handling done so that api calls and actions are in a try, and the catch rethrows a custom Exception to handle if an exception has been thrown in in the try.

This all works fine and dandy. Exceptions are reported to new relic but I only want unhandled exceptions to arrive in new relic. My understanding from reading the Laravel docs was that there are a few choices:

$exceptions->dontReport[..]

This stops exception reaching new relic but also means I cant still log an info to keep track to see if it is happening often)

$exceptions->report(function(CustomException::class) {... // do logging etc})

This allows for logging of the exception if I want and any custom bits but it still reaches new relic even with the appended $exceptions->report()->stop();

Use the report() method in the Exception and return true

Based on the Laravel documentation https://laravel.com/docs/11.x/errors#renderable-exceptions

/**
 * Report the exception.
 */
public function report(): bool
{
    if (/** Determine if the exception needs custom reporting */) {
 
        // ...
 
        return true;
    }
 
    return false;
}

it makes it seem that if I want to check the underlying cause of the exception I can and then based off that do my own logging etc or return false and let it continue down the usual path in the ExceptionHandler but returning true still gets the exception into new relic.

Deep Dive

While seeing how Laravel handles these exceptions I went into the Handler class in Illuminate\Foundation\Exceptions\Handler::class and in there is the ->report() method. When I follow that through I can't understand why return true does not behave the same as the don't report array. Because if the exception thrown has a report method and when called the report method returns true it returns early just like the dontReport array.

TLDR

I have custom exceptions and inside the custom exceptions I want to use the report() and render() method to handle what happens with them rather than cluttering up the bootstrap/app.php and making new relic noisy for non user breaking reasons. dontReport stops new relic but doesn't give logging and return true from report() still gets to new relic even though from my digging I dont see how. Is this possible? Am I missing a key bit of how the Laravel Exception Handler works? Does anyone with any new relic use cases know if it's just a config thing and if I change the new relic report level it wont bubble info logs from the report? Thanks in advance and understand this is a chunky question.

0 likes
3 replies
LaryAI's avatar
Level 58

To address the issue of custom exception handling in Laravel and prevent certain exceptions from being reported to New Relic while still allowing for custom logging, you can follow these steps:

  1. Custom Exception Class: Ensure your custom exception class has a report method. This method should return true if you want to prevent the exception from being reported to New Relic.

  2. Custom Logging: Inside the report method, you can perform any custom logging you need. This allows you to log the exception details without it being reported to New Relic.

  3. Exception Handler Configuration: In your App\Exceptions\Handler class, you can use the reportable method to define custom reporting logic for your exceptions.

Here's a step-by-step implementation:

Step 1: Define the Custom Exception

Create a custom exception class with a report method:

<?php

namespace App\Exceptions;

use Exception;

class CustomException extends Exception
{
    /**
     * Report the exception.
     *
     * @return bool
     */
    public function report(): bool
    {
        // Perform custom logging
        \Log::info('Custom exception occurred: ' . $this->getMessage());

        // Return true to prevent the exception from being reported to New Relic
        return true;
    }
}

Step 2: Configure the Exception Handler

In your App\Exceptions\Handler class, you can use the reportable method to define custom logic for reporting exceptions:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * Register the exception handling callbacks for the application.
     */
    public function register()
    {
        $this->reportable(function (CustomException $e) {
            // This will prevent the exception from being reported to New Relic
            return false;
        });
    }
}

Explanation

  • Custom Logging: The report method in your custom exception class allows you to log the exception details using Laravel's logging facilities. This ensures you have a record of the exception without it being sent to New Relic.

  • Prevent Reporting: By returning true in the report method of your custom exception, you signal to Laravel that the exception has been handled and should not be reported further.

  • Handler Configuration: The reportable method in the Handler class allows you to define custom logic for specific exceptions. By returning false, you ensure that the exception is not reported to New Relic.

This setup should help you achieve the desired behavior of logging custom exceptions without them being reported to New Relic, while still allowing for custom logging and handling.

adamdavies's avatar

Unfortunately I've tried all of the above and custom exceptions are still hitting the new relic, if anyone has any idea why, that would be greate.

Artwork's avatar

What if the notification is sent by an event handler, that is out of your initial scope? Have you tried tracing the call? For instance, using breakpoints, step-by-step? There are options like XDebug and SPX.

Please or to participate in this conversation.