Framework: Throttle custom message

Created on 19 May 2016  路  12Comments  路  Source: laravel/framework

Hi,
I want to use custom message for API Rate Limiter and added this exception in my exception handler:

public function render($request, Exception $e)
    {
        if($e instanceof TooManyRequestsHttpException) {
            return response()->json([
                'message' => 'Too many requests. Slow your roll!',
                'code' => 429
            ], 429);
        }

        return parent::render($request, $e);
    }

but it show me again this message "Too Many Attempts."

Most helpful comment

use Symfony\Component\HttpKernel\Exception\HttpException;
...
if($exception instanceof HttpException && $exception->getStatusCode() == 429) {
return response()->json([
'message' => 'Too Many Attempts',
'code' => 429
], 429)->withHeaders($exception->getHeaders());
}

All 12 comments

Laravel doesn't use TooManyRequestsHttpException, if you want to use a custom message you can extend ThrottleRequests and override buildResponse().

@mehdibahraami Thanks for getting in touch, but I'm afraid we can only process framework bugs here.

@themsaid This is probably with reference to my throttle package. ;)

Oh, I see. Graham's packages everywhere :)

@mehdibahraami It's likely you forgot to import the correct class. Feel free to ask on the forums for help. We can't process things like this on our GitHub issues.

@GrahamCampbell I was using the Laravel throttle and i thought your package and the Laravel package is the same. For this reason i asked my question in your package issues.
This is not Laravel problem? I can't send personalized response(for my client) directly and must extend a package and changed it? Other people have made this request before:
https://github.com/laravel/framework/pull/13452
At the end, I recently started work with Laravel and i think it is better to be a little kinder to respond to users ;)

@themsaid Thank you so much.

Hello Folks, Recently I've read this article and it works for me...

https://thedevsaddam.gitbooks.io/off-time-story/content/how_to_customize_laravel_throttle_message_response.html

I hope this will help You..!!

A more simple way:

Just create a new middleware in app\Http\Middleware and extend \Illuminate\Routing\Middleware\ThrottleRequests, then override the buildResponse method.

use Symfony\Component\HttpKernel\Exception\HttpException;
...
if($exception instanceof HttpException && $exception->getStatusCode() == 429) {
return response()->json([
'message' => 'Too Many Attempts',
'code' => 429
], 429)->withHeaders($exception->getHeaders());
}

hi @themsaid I tried to override buildResponse() but it is missing in \Illuminate\Routing\Middleware\ThrottleRequests I tried to use buildException() instead but it doesn't work.

Please give me the right way to build max throttle custom message.

I'm using Laravel 5.6

I have tested this on Laravel 5.6

You can do it by ThrottleRequestsException, via app/Exceptions/Handler.php

use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Http\Response;

and next add it to your render() function

    public function render($request, Exception $e)
    {
        if ($e instanceof ThrottleRequestsException) {
            return $this->response409($e);
        }

        return parent::render($request, $e);
    }
    public function response409(Exception $e)
    {
        $errors = new MessageBag();
        $errors->add("message", "409 Too many requests");
        return response()->make(view('errors.409')->withErrors($errors), Response::HTTP_CONFLICT);
    }

if($exception instanceof ThrottleRequestsException) {
return response()->json([
'message' => 'Too many requests. Slow your roll!',
'code' => 429
], 429);
return parent::render($request, $exception);
}
add this in render function and use ThrottleRequestsException; in app/Extensions/Handler.php

Was this page helpful?
0 / 5 - 0 ratings