How do I handle a 429 error (or any error in fact), when working with Inertia forms? I tried the callback onError, but it didn't work:
post(route("verification.send"), {
onSuccess: () => {
console.log("verification email sent");
},
onError(error) {
console.log("couldn't resend verification email");
console.log(error);
},
});
What happens instead is a repaint, with Laravel's standard 429 | Too many requests error screen. I figured this is because I need a JSON response, so I tried creating a middleware, but to no avail:
class EnsureJsonResponse
{
public function handle(Request $request, Closure $next)
{
$request->headers->set('Accept', 'application/json');
$request->headers->set('Content-Type', 'application/json');
return $next($request);
}
}
then in bootstrap/app.php:
$middleware->appendToGroup('json.response', [
\App\Http\Middleware\EnsureJsonResponse::class,
]);
then on route:
Route::post('email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
->middleware(['json.response', 'throttle:1,0.5'])
->name('verification.send');
Anyone know what the actual problem is?