Skip to content

fix: normalize non-2xx HTTP responses into ApiError objects#23

Open
dev-khimesh wants to merge 1 commit into
2lll5:mainfrom
dev-khimesh:feat/api-error-handling
Open

fix: normalize non-2xx HTTP responses into ApiError objects#23
dev-khimesh wants to merge 1 commit into
2lll5:mainfrom
dev-khimesh:feat/api-error-handling

Conversation

@dev-khimesh

Copy link
Copy Markdown

Summary

Harden API response error handling: non-OK HTTP responses are now normalized into ApiError objects, run through the existing error interceptor chain, and thrown consistently.

Changes

Before

request<T>() returned every HTTP response as a successful ApiResponse<T>, even 4xx/5xx responses. Callers had to duplicate status checks, and the error interceptor chain (401, 429 handlers) was never exercised for HTTP errors.

After

  • request<T>() checks response.ok — non-2xx responses are parsed into ApiError and thrown
  • ✅ Structured JSON error bodies (with message, error, details, suggestion fields) are safely extracted
  • ✅ Text error bodies fall back gracefully
  • ✅ Error interceptors run for HTTP errors — 401 triggers auth refresh, 429 triggers rate-limit backoff
  • ✅ Request ID and path are preserved in the error object
  • ✅ Status-based suggestions guide users (401 → login, 429 → wait, 500 → try later)
  • ✅ Timeout and network errors continue to use existing normalizeError handling
  • ✅ Retry logic correctly skips non-2xx HTTP errors (not status codes) while preserving timeout retries
  • ✅ Existing 2xx success path is unchanged

Required Validation

Closes #1

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.

[$40 BOUNTY] [TypeScript] Harden API response error handling

1 participant