Overview
The Convex Rate Limiter provides two approaches to handling rate limit violations:- Manual handling: Check the
okproperty and handle errors yourself - Automatic errors: Use
throws: trueto automatically throw errors
Manual Error Handling (Default)
By default,limit() returns { ok, retryAfter } and never throws:
Automatic Error Throwing
Usethrows: true to automatically throw a ConvexError when the rate limit is exceeded:
“It throws aConvexErrorwithRateLimitErrordata (data: {kind, name, retryAfter}) instead of returning whenokis false.”
Real Example from Source Code
Fromexample/convex/example.ts:
The RateLimitError Type
Whenthrows: true is used, the error data follows this structure:
Using isRateLimitError Helper
The library provides a type guard to check if an error is a rate limit error:Implementation
Fromsrc/client/index.ts:35-42:
ConvexError Integration
Rate limit errors are thrown asConvexError instances, which means they:
- Are automatically serialized and sent to the client
- Include structured data that clients can parse
- Work seamlessly with Convex’s error handling
Client-Side Error Handling
When rate limit errors reach the client, you can handle them in your React components:Choosing the Right Approach
- Use throws: true when...
- Use manual handling when...
✅ Use automatic errors when:
- Rate limiting is a hard requirement (security, abuse prevention)
- You want concise code without explicit checks
- The operation should always fail when rate limited
- You’re protecting against abuse (failed logins, spam)
Combining Multiple Rate Limits
You can combine multiple rate limits with different error handling strategies:Best Practices
Always handle rate limit errors on the client
Always handle rate limit errors on the client
Even with
throws: true, ensure your client code handles the error:Provide helpful retry information
Provide helpful retry information
Convert milliseconds to user-friendly units:
Log rate limit violations
Log rate limit violations
Monitor rate limit hits to detect abuse or adjust limits:
Next Steps
- Learn about Checking Limits without consuming tokens
- Understand Resetting Limits after successful actions
- Explore Basic Usage for getting started