Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Deduplicate exceptions #48288

Merged
merged 4 commits into from
Sep 6, 2023

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Sep 4, 2023

Adds the ability to deduplicate exception reporting.

Exceptions may sometimes be caught, reported, re-thrown only to be re-reported at a higher level - potentially a few times throughout the application layers.

This adds a lever to the exception handler to deduplicate exceptions to ensure a single instance of an exception is only reported on time.

use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        // …

        $this->callAfterResolving(ExceptionHandler::class, function ($handler) {
            $handler->deduplicateReporting();
        });
    }
}

If the same exception is now reported more than once, it will only actually report a single time.

$e = new RuntimeException('abc');

report($e);

report($e);

try {
    throw $e;
} catch (Exception $exception) {
    report($exception);
}

// only reported once

There is already precedent for this kind of feature in the framework with something like middleware.

Route::middleware(MyMiddleware::class)->group(function () {
     Route::get(/* ... */)->middleware(MyMiddleware::class);
});

The MyMiddleware is not executed twice, as the framework deduplicates them for us.

@timacdonald timacdonald marked this pull request as draft September 4, 2023 07:06
@timacdonald timacdonald marked this pull request as ready for review September 4, 2023 07:32
@nunomaduro
Copy link
Member

I understand the reasoning; however, this doesn't seem like good practice to me. I believe this issue should be resolved at the application level.

Additionally, it raises other questions; as the WeakMap needs to be cleared between requests or between jobs, etc.

@timacdonald
Copy link
Member Author

As a WeakMap doesn't hold a reference to the object, we don't need to clear anything. The exception will be garbage collected in the same way it currently is.

@mpyw
Copy link
Contributor

mpyw commented Sep 4, 2023

Clever solution with WeakMap 👍

@taylorotwell taylorotwell merged commit 2e3d23f into laravel:10.x Sep 6, 2023
@rodrigopedra
Copy link
Contributor

Very nice addition @timacdonald ... I implemented a very similar solution 2 weeks ago to an app a client's was always asking about exception's duplicates...

Unfortunately it is still a Laravel 8 app, as it still runs on PHP 7.4 on centOS, so I couldn't use a WeakMap.

This client is planning to migrate to a newer server later this year, so knowing you already got this covered makes me very happy and eager to finally migrate this app to a newer Laravel version.

Thanks a lot! Have a nice day =)

@timacdonald
Copy link
Member Author

Really appreciate that feedback, @rodrigopedra ❤️

Hopefully once you get moved over and updated you can delete some code 🎉

@rodrigopedra
Copy link
Contributor

Hey @timacdonald I told my client about this PR, and he asked me about some other features we might be missing or reimplementing, and... we are migrating the server this weekend to Rocky 9, PHP 8.2 and Laravel 10!

well I will be working on migrating the codebase tomorrow and Friday, and he prefers to manage his servers himself

So I guess I own you a second thank you =)

(and need to stop posting on this thread before being called a spammer 🤐)

Thanks again, and may God bless you. Have a nice day!

@timacdonald
Copy link
Member Author

yo, @rodrigopedra, if this is spam - sign me up!

This is amazing to hear. I love that this feature helped move the discussion forward. Very awesome ☺️

@timacdonald timacdonald deleted the deduplicate-exceptions branch September 7, 2023 04:54
coppolafab pushed a commit to coppolafab/laravel-framework that referenced this pull request Sep 8, 2023
* Deduplicate exceptions

* Fix test

* lint

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants