Skip to main content

Overview

The Convex Rate Limiter provides two approaches to handling rate limit violations:
  1. Manual handling: Check the ok property and handle errors yourself
  2. Automatic errors: Use throws: true to automatically throw errors

Manual Error Handling (Default)

By default, limit() returns { ok, retryAfter } and never throws:
This approach gives you full control over the error response.

Automatic Error Throwing

Use throws: true to automatically throw a ConvexError when the rate limit is exceeded:
From the README:
“It throws a ConvexError with RateLimitError data (data: {kind, name, retryAfter}) instead of returning when ok is false.”

Real Example from Source Code

From example/convex/example.ts:

The RateLimitError Type

When throws: 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

From src/client/index.ts:35-42:

ConvexError Integration

Rate limit errors are thrown as ConvexError 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 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

Even with throws: true, ensure your client code handles the error:
Convert milliseconds to user-friendly units:
Monitor rate limit hits to detect abuse or adjust limits:

Next Steps