Wrap network errors in ApiError for consistent error handling #114
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.
Summary
Wrap native fetch errors in
ApiErrorobjects for consistent error handling across the application.Problem
When the API server is unreachable (e.g., wrong URL, server down, CORS issues),
fetch()throws a nativeTypeError: Failed to fetch. This breaks the error handling pattern used throughout the codebase, which expectsApiErrorobjects withstatusCodeandmessageproperties.Solution
Catch network errors from
fetch()and wrap them inApiError:statusCode: 0indicates network-level failure (distinct from HTTP status codes ≥100)message: "Network error: {original message}"preserves the original error for debuggingAdd JSDoc documentation to
ApiError.statusCodeexplaining that 0 means network errorBreaking Change
TypeErrorwithmessage: "Failed to fetch"ApiErrorwithstatusCode: 0,message: "Network error: Failed to fetch"Code using
error instanceof TypeErrorwill need adjustment. Analysis ofservicesrepo shows no such patterns.Files Changed
packages/react/src/hooks/api.hook.ts- wrap fetch errorspackages/react/src/definitions/error.ts- document statusCode: 0Test Plan
REACT_APP_API_URLto unreachable endpoint (e.g.,http://localhost:9999)Network error: Failed to fetcherror.statusCode === 0