Improve HTTP Error Handling with Descriptive Messages #24
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.
📝 Description
This PR replaces raw, generic catch-all errors with strongly-typed, descriptive HTTP error classes. Instead of logging opaque JSON or stack traces, callers can now
instanceofspecific errors and get:status_codeproperty for programmatic handlingOriginal Example from SDK Documentation
The SDK docs show the following snippet, but accessing
error.statuswill always beundefinedbecause caught errors are typed asunknown:In practice,
error.statusdoes not exist and evaluating it yieldsundefined.Before
✨ Benefits
error instanceofinstead of parsing status codes.🎯 Status Codes Covered
V0BadRequestErrorV0AuthErrorV0PaymentRequiredErrorV0ForbiddenErrorV0NotFoundErrorV0TimeoutErrorV0UnprocessableEntityErrorV0RateLimitErrorV0InternalServerErrorV0ServiceUnavailableErrorV0GatewayTimeoutErrorV0UnknownErrorExplains the purpose and usage of the
createHttpErrorfunction, which is not exported in the public SDK API. It serves purely as an internal helper during development.Factory function that generates custom
Errorsubclasses with:nameproperty reflecting the error typestatus_codeproperty for programmatic checksUsage (development)
Internally, we use it to define all HTTP error classes:
🎯 TypeScript Support
V0Error Union Type
For better TypeScript integration, especially with React state management, we provide a union type that includes all possible V0 errors:
🧪 Testing