refactor(api): centralize error-to-message mapping - #620
Open
Marvelg256 wants to merge 5 commits into
Open
Conversation
Add mapApiError() helper in src/lib/mapApiError.ts that translates
unknown error values (ApiError shapes, Error instances, non-Error
rejections) to a consistent { message, requestId } structure.
Replace scattered inline error-to-message logic across 15 call sites
with this single tested helper.
Changes:
- New: src/lib/mapApiError.ts — mapApiError(error, fallback?) helper
- New: src/lib/__tests__/mapApiError.test.ts — 26 comprehensive tests
- Updated: src/lib/usePolling.ts — replace errorMessage()
- Updated: src/lib/useApi.ts — use mapApiError for rate-limited/generic
- Updated: src/lib/useApiMutation.ts — use mapApiError for message extraction
- Updated: src/app/usage/page.tsx — replace describeError() with mapApiError
- Updated: 9 additional page-level catch blocks (webhooks, search,
api-keys, admin, export, services, agents, events, service detail/edit)
- Fixed: pre-existing bug in api-keys/page.tsx where 'error' was undefined
Test output:
src/lib/__tests__/mapApiError.test.ts — 26/26 passed
Targeted call-site tests — 264/265 passed (1 pre-existing failure)
Full suite — 92/95 suites pass (3 pre-existing failures)
Preserves all existing fallback messages verbatim:
"failed to load", "failed to mutate", "Search failed", "Export failed"
Closes Agentpay-Org#354
The onboarding and reports layouts reference pageTitles.onboarding and pageTitles.reports but those keys were never added to the object.
UsageRow, PresetKey, PRESET_RANGES, toISODate, buildDateRangeAnnouncement, UsageDateRangeFilters, UsageQueryRows, and deriveUsageRows were all referenced but never imported. Also adds the missing useMemo for queryRows.
The original code had a stray }, []); after applyPreset that was a syntax error from a broken useCallback wrapper. Fixing the syntax error revealed that the memoization contract required useCallback to keep the onPresetChange prop stable across re-renders.
… update The 'uses the page from searchParams' test was calling getByText synchronously after waitFor for the API call. React batches the setItems/setLoading state updates from the mock resolution, so the DOM wasn't updated yet. Wrapping in waitFor gives React time to flush.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add mapApiError() helper in src/lib/mapApiError.ts that translates unknown error values (ApiError shapes, Error instances, non-Error rejections) to a consistent { message, requestId } structure. Replace scattered inline error-to-message logic across 15 call sites with this single tested helper.
Changes:
Test output:
src/lib/tests/mapApiError.test.ts — 26/26 passed Targeted call-site tests — 264/265 passed (1 pre-existing failure) Full suite — 92/95 suites pass (3 pre-existing failures)
Preserves all existing fallback messages verbatim:
"failed to load", "failed to mutate", "Search failed", "Export failed"
Closes #354
Summary
Related issue
Type of change
Local checks
Paste the result or explain why a docs-only change did not require the command.
npm run lintnpm run typechecknpm testnpm run buildCoverage and tests
Accessibility and UI review
Security review
.envfiles, or generated artifacts are committed.Notes for reviewers