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

Aperta Adatta ai principianti
#266 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 5 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
ruby

Direzione di ricerca

Inizia da lib/resend/errors.rb e lib/resend/request.rb, poi controlla i test esistenti per l’inizializzazione degli errori e Request#handle_error!. L’issue propone di esporre lo stato HTTP e mantenere il nome dell’errore della risposta, preservando la compatibilità con le chiamate esistenti al costruttore; decidi se includere le mappature facoltative delle classi di errore. Il lavoro è completato quando i chiamanti possono leggere i dettagli di errore aggiunti e i test coprono la gestione degli errori esistente e quella nuova.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Ruby
Stelle
55
Fork
12
Merge medio
4g 2h
PR unite (30g)
1

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di resend/resend-ruby

Tutte le issue di resend/resend-ruby

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.