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
instanceof
specific errors and get:status_code
property for programmatic handlingOriginal Example from SDK Documentation
The SDK docs show the following snippet, but accessing
error.status
will always beundefined
because caught errors are typed asunknown
:In practice,
error.status
does not exist and evaluating it yieldsundefined
.Before
✨ Benefits
error instanceof
instead of parsing status codes.🎯 Status Codes Covered
V0BadRequestError
V0AuthError
V0PaymentRequiredError
V0ForbiddenError
V0NotFoundError
V0TimeoutError
V0UnprocessableEntityError
V0RateLimitError
V0InternalServerError
V0ServiceUnavailableError
V0GatewayTimeoutError
V0UnknownError
Explains the purpose and usage of the
createHttpError
function, which is not exported in the public SDK API. It serves purely as an internal helper during development.Factory function that generates custom
Error
subclasses with:name
property reflecting the error typestatus_code
property 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