[5.6] Throttle requests exception - #23358
Conversation
| try { | ||
| $this->withoutExceptionHandling()->get('/'); | ||
| } catch (Throwable $e) { | ||
| $this->assertTrue($e instanceof ThrottleRequestsException); |
There was a problem hiding this comment.
You can use assertInstanceOf instead
|
I personally like the idea - but this is a breaking change. Someone might be relying on the |
|
I'm not sure there is any breaking change here if it extends HttpException. Previous try / catch will still work and it won't be logged either since that is an instanceof check and it will still see the HttpException type. Am I wrong? |
|
Ah yes, good point @taylorotwell -- you are correct. |
|
@taylorotwell it will still catch it because of inheritance. |
|
@bencomeau - now I’m confused. If the whole point of this PR was to allow for ThrottlingExceptions to be logged, but they won’t be logged because of inheritance of HttpException - then that means this PR does not work as originally described? What am I missing?
|
|
TooManyRequestsHttpException already exists though... |
|
@laurencei Thanks, hopefully I can add some details to help clear up confusion! With this functionality, users can:
Beyond that, I suppose the bottom-line goal was to abstract the exception a bit so it could more fluidly be handled. @GrahamCampbell Thank you. I did notice this class existed in Symfony however given their handling of the parameters, I thought it was easier/cleaner to implement a different class. This is mostly based on the first parameter of the |
|
thanks @bencomeau |
I feel strongly it should. It is wrong for laravel to add it's own exception when one already exists, and should have been used. |
|
I think it was a mistake for symfony to make their http exception class not abstract tbh. |
|
@GrahamCampbell Okay, thanks for the feedback. I do understand why you strongly feel it should -- makes enough sense to me to not reinvent the wheel :) @taylorotwell Do you agree? Should I update this PR to extend Symfony's |
|
@bencomeau it has already been merged in you need to create a new one. |
Feature
I am proposing that the
ThrottleRequestsmiddleware throw a more specific exception as opposed to the current\Symfony\Component\HttpKernel\Exception\HttpException.Reasoning
The
$dontReportproperty of the exception handler class contains the\Symfony\Component\HttpKernel\Exception\HttpException::classwhich means by default rate limiting will not be logged. If one wanted to log these events, they would need to update thereportmethod of the exception handler class to something like:Furthermore, I don't believe they would be able to use Reportable Exceptions in this instance since the exception is thrown in the framework's middleware.
Solution
Create a new exception which the
ThrottleRequestsmiddleware can utilize; one that simply extends the\Symfony\Component\HttpKernel\Exception\HttpExceptionclass. Add an additional test to theThrottleRequestsTestto confirm the middleware is indeed throwing the desired exception.Final Thoughts
If approved, we may consider adding
\Illuminate\Http\Exceptions\ThrottleRequestsException::classto the$dontReportproperty of the exception handler class. Otherwise, this update would remove the long-standing functionality of not logging rate limiting by default. I would submit a pull request to thelaravelrepository depending on the outcome of this pull request.