Skip to content

fix: harden API response error handling - #24

Open
gtmdev-br wants to merge 10 commits into
2lll5:mainfrom
gtmdev-br:fix/harden-api-error-handling
Open

fix: harden API response error handling#24
gtmdev-br wants to merge 10 commits into
2lll5:mainfrom
gtmdev-br:fix/harden-api-error-handling

Conversation

@gtmdev-br

Copy link
Copy Markdown

Summary

Harden the shared frontend API client so non-2xx HTTP responses are rejected as errors instead of flowing through as successful ApiResponse<T> values. This ensures error interceptors (401, 429) fire reliably and callers no longer need to duplicate status checks.

Changes

  • Added parseErrorResponse() — safely parses JSON and text error response bodies into ApiError objects, preserving requestId, details, path, and actionable suggestion fields.
  • Modified request<T>() — checks response.ok after fetch; non-2xx responses are parsed, run through the error interceptor chain, and thrown immediately (bypassing retry logic since HTTP errors are deterministic).
  • Added isApiError() type guard — distinguishes our ApiError objects from native Error types (DOMException, TypeError) to prevent false positives in the retry logic.
  • Added validation test suite — 8 tests covering: 200 OK success, 401 JSON error, 429 rate-limit, 500 text error, 404 not-found, error interceptor execution, timeout (408), and network failure.
  • Included diagnostic build artifactdiagnostic/build-c6be4f51.json

Testing

  • npx tsc --noEmit — passes (zero errors)
  • npm run build — builds successfully
  • npx tsx src/services/__tests__/api-error-handling.test.ts — 8/8 tests pass
  • Verified: 2xx returns ApiResponse, 401/429/500/404 throw ApiError with correct codes and suggestions, error interceptors fire, timeout returns 408, network errors handled

Checklist

  • Frontend module builds locally
  • Validation tests pass locally
  • Diagnostic build log is committed in this PR
  • Changes are scoped to the PR purpose
  • Security, privacy, and error-handling implications have been considered

Closes #1

lobster-trap and others added 10 commits June 17, 2026 02:47
Verified scoped implementation and script-generated diagnostic artifacts for bounty issue 123a-bcd#1.
- Add configurable max cardinality limit for metric tags (default: 1000)
- Enforce limit before samples are queued to prevent DB growth
- Track unique tag combinations per metric name deterministically
- Add WithMaxCardinality option for configuration
- Update CollectorStats to expose cardinality metrics
- Add comprehensive tests for cardinality limiting behavior

Closes 123a-bcd#14
- Add parseErrorResponse() to safely parse JSON and text error bodies into ApiError
- Modify request<T>() to check response.ok and throw on non-2xx HTTP statuses
- Add isApiError() type guard to distinguish API errors from native Error types
- HTTP errors bypass retry logic (they are deterministic, not transient)
- Error interceptors (401, 429) now reliably fire for HTTP error responses
- Add validation test suite covering: 200, 401, 429, 500, 404, timeout, network errors
- Include diagnostic build artifact

Closes 123a-bcd#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

3 participants