Skip to content

Add error handler for RateLimit error#128

Merged
drish merged 3 commits intoresend:mainfrom
NerdyBoyCool:feature/add-handling-rate-limit
Oct 1, 2025
Merged

Add error handler for RateLimit error#128
drish merged 3 commits intoresend:mainfrom
NerdyBoyCool:feature/add-handling-rate-limit

Conversation

@NerdyBoyCool
Copy link
Contributor

@NerdyBoyCool NerdyBoyCool commented Oct 1, 2025

ref: #31
ref: https://resend.com/docs/api-reference/rate-limit

Thank you for developing Ruby SDK.

Summary

When calling the Resend API from Ruby/Rails applications, we need to properly handle rate limit errors. Currently, RateLimitExceededError doesn't provide information.

This PR enhances rate limit error handling by adding detailed rate limit information to RateLimitExceededError.

Changes

  • Enhanced RateLimitExceededError class: Added attributes to expose rate limit metadata:
    • rate_limit_limit: Maximum number of requests allowed
    • rate_limit_remaining: Number of requests remaining in current window
    • rate_limit_reset: Timestamp when the rate limit resets
    • retry_after: Number of seconds to wait before retrying
  • Updated error initialization: Modified Resend::Error base class to accept and store response headers,
    enabling errors to access rate limit information from API responses
  • Simplified error handling: Refactored handle_error! method in lib/resend/request.rb to streamline error
    instantiation with headers
  • Added comprehensive tests: Updated specs to verify that RateLimitExceededError correctly parses and exposes
    rate limit headers

Summary by cubic

Add detailed rate limit data to RateLimitExceededError and propagate response headers when raising errors. Apps can now read limit, remaining, reset, and retry-after to implement proper backoff.

  • New Features
    • Expose rate_limit_limit, rate_limit_remaining, rate_limit_reset, retry_after on RateLimitExceededError.
    • Store response headers on Resend::Error; handle_error! now passes headers to all errors.
    • Updated specs to verify header parsing and exposed attributes.

@NerdyBoyCool
Copy link
Contributor Author

@drish
Hello. Colud you review this PR?

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 3 files

Prompt for AI agents (all 2 issues)

Understand the root cause of the following 2 issues and fix them.


<file name="lib/resend/errors.rb">

<violation number="1" location="lib/resend/errors.rb:24">
RateLimitExceededError needs to pass the headers up to Error#initialize so error.headers exposes the actual response headers.</violation>
</file>

<file name="lib/resend/request.rb">

<violation number="1" location="lib/resend/request.rb:50">
`resp[:headers]` reads the parsed JSON body, not the HTTP headers exposed by HTTParty, so the rate limit metadata will always be empty. Please pull the headers from `resp.headers` instead so RateLimitExceededError receives the real values.</violation>
</file>


Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Ask questions if you need clarification on any suggestion

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

NerdyBoyCool and others added 2 commits October 1, 2025 18:25
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
@drish drish self-requested a review October 1, 2025 14:09
@drish
Copy link
Member

drish commented Oct 1, 2025

this is great @NerdyBoyCool , thank you for your contribution !

@NerdyBoyCool
Copy link
Contributor Author

@drish
Thank you for the quick review! 🙇

I'll leave the timing of releases and merges to the repository owner!

@drish
Copy link
Member

drish commented Oct 1, 2025

@drish Thank you for the quick review! 🙇

I'll leave the timing of releases and merges to the repository owner!

ill merge this and have a release going soon

@drish drish merged commit 1c0e932 into resend:main Oct 1, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants