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

[11.x] Add option to report throttled exception in ThrottlesExceptions middleware #50896

Merged
merged 2 commits into from
Apr 3, 2024

Conversation

JaZo
Copy link
Contributor

@JaZo JaZo commented Apr 3, 2024

Description

This PR adds an option to the ThrottlesExceptions (and ThrottlesExceptionsWithRedis) middleware to have the throttled exception be reported. This will allow you to see the exception in your logging and be aware that something is not going as expected.

Usage

Report every throttled exception:

use Illuminate\Queue\Middleware\ThrottlesExceptions;

/**
 * Get the middleware the job should pass through.
 *
 * @return array<int, object>
 */
public function middleware(): array
{
    return [(new ThrottlesExceptions(10, 10))->report()];
}

Report only specific throttled exceptions:

use Illuminate\Http\Client\HttpClientException;
use Illuminate\Queue\Middleware\ThrottlesExceptions;

/**
 * Get the middleware the job should pass through.
 *
 * @return array<int, object>
 */
public function middleware(): array
{
    return [(new ThrottlesExceptions(10, 10))->report(fn (Throwable $throwable) => $throwable instanceof HttpClientException)];
}

N.B. I'll make a PR to the docs once this gets merged.

Motivation and context

I had one specific job that kept failing because of the max tries/timeout that was reached. I couldn't see why because I only saw the MaxAttemptsExceededException in my logging and nothing else. After debugging and seeing that the ThrottlesExceptions middleware we used, discards the exception, I disabled the middleware and I got the actual exception; it was trying to insert null in a non-nullable column. Afterwards I configured the middleware to only throttle specific exceptions, but that posibility wasn't documented, so I wasn't aware of this feature. So I also opened laravel/docs#9551 to document that. However, I still think this reporting feature is worth it and saves you a lot of debugging time!

@taylorotwell taylorotwell merged commit 552bdee into laravel:11.x Apr 3, 2024
28 checks passed
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.

None yet

2 participants