Reputation: 936
I am using Laravel 5.7 version. I am using throttle
in Kernel.php
to avoid user to send queries more than 60. I would like to translate its message "Too Many Attempts." and use own message. how can I do it in laravel? Where can I find that?
Upvotes: 0
Views: 1519
Reputation: 4201
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());
}
Upvotes: 2
Reputation: 3567
You could create your custom Middleware in the app/Http/Middlewares
folder, extend the base \Illuminate\Routing\Middleware\ThrottleRequests
class and override the buildException
method (original implementation here).
Then assign throttle
Middleware to your custom Middleware class in Kernel.php
Upvotes: 1
Reputation: 382
In your Laravel exception handler you can handle that exception before rendering and replace that exception with your custom exception.
In app/Exceptions/Handler.php
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*/
public function render($request, Exception $exception)
{
if($exception instanceof ThrottleRequestsException) {
return parent::render(
$request, new ThrottleRequestsException(
'Your message',
$exception->getPrevious(),
$exception->getHeaders(),
$exception->getCode()
)
);
}
return parent::render($request, $exception);
}
Upvotes: 2