Expose an error's HTTP status and Resend error name on `Resend::Error`

Open Beginner friendly
#266 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 5 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
ruby

Research direction

Start with lib/resend/errors.rb and lib/resend/request.rb, then check existing tests for error initialization and Request#handle_error!. The issue proposes exposing the HTTP status and preserving the response error name while keeping existing constructor calls compatible; decide whether to include the optional error class mappings. Done when callers can read the added error details and tests cover existing and new error handling.

Written by the indexing model from the issue text.

Description

Summary

A caller can't tell apart the errors the gem raises as the same class without reaching into the exception's internals. Resend::Error is given the response's HTTP status and keeps it in @code, but defines no reader for it. Request#handle_error! keeps the response's message and drops its name (invalid_idempotent_request, concurrent_idempotent_requests, …) entirely.

lib/resend/errors.rb:

attr_reader :headers

def initialize(msg, code = nil, headers = {})
  super(msg)
  @code = code      # stored, but there's no reader
  @headers = headers
end

lib/resend/request.rb:

def handle_error!(data, resp = nil)
  code = data[:statusCode]
  body = data[:message]   # data[:name] isn't kept
  # ...
  error_class = Resend::Error::ERRORS[code] || Resend::Error
  raise error_class.new(body, code, headers)
end
Where it bites

ERRORS has no entry for 409 or for 502–504, so each of those raises the base Resend::Error, with nothing public to tell them apart:

  • The four 409s call for different handling. The error reference documents concurrent_idempotent_requests (try again later, with the same key), invalid_idempotent_request (change the key or the payload), resource_locked and forge_not_enabled. A caller sending with an idempotency_key and retrying after a timeout has to know which one it got: retrying under the same key is right for the first and refused for 24 hours for the second.
  • A 502, 503 or 504 with a JSON body raises the same base class as those 409s, so a transient outage can't be told from a refusal no retry fixes. (A body that isn't JSON becomes InternalServerError through check_json!.)

Today the only ways to tell are error.instance_variable_get(:@code) and matching error.message, whose wording isn't a contract.

For comparison

resend-node answers every error with { message, statusCode, name } (ErrorResponse in src/interfaces.ts), name typed against the documented codes.

Possible directions
  1. attr_reader :code on Resend::Error, returning the status it's already given. Additive.
  2. Keep the response's name on the error, say as attr_reader :name, matching resend-node's field, passed by handle_error! as a new optional argument so existing Resend::Error.new(msg, code, headers) calls keep working.
  3. Optionally, classes for the statuses that fall through today: a ConflictError for 409, and 502–504 mapped to ServerError subclasses.

1 and 2 are the smallest change and would be enough on their own.

Dominant language
Ruby
Stars
55
Forks
12
Avg merge
4d 2h
Merged PRs (30d)
1

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from resend/resend-ruby

All issues in resend/resend-ruby

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.