Repository navigation
feat(ai-client): export StreamHttpError from ai-client and framework packages #2258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| '@tanstack/ai-client': minor | ||
| '@tanstack/ai-react': minor | ||
| '@tanstack/ai-solid': minor | ||
| '@tanstack/ai-preact': minor | ||
| '@tanstack/ai-vue': minor | ||
| '@tanstack/ai-octane': minor | ||
| '@tanstack/ai-remix': minor | ||
| '@tanstack/ai-svelte': minor | ||
| '@tanstack/ai-angular': minor | ||
| --- | ||
|
|
||
| Export `StreamHttpError` from `@tanstack/ai-client` and re-export it from the framework packages. The fetch and XHR chat transports throw this class when the response status is not 2xx. It has `status`, and `retryAfterMs` when the response has a valid `Retry-After` header. Import it to check the status with `instanceof` in `onError` instead of parsing the error message. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,6 +152,37 @@ export function ChatError() { | |
|
|
||
| If the connection drops in the middle of a line, the stream transports throw `StreamTruncatedError`. | ||
|
|
||
| When the response status is not 2xx, the fetch and XHR transports throw `StreamHttpError`. Check for it in `onError`: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document the BYOK 401 exception. When a 401 response has a BYOK-missing body, the fetch and XHR adapters throw
|
||
|
|
||
| ```tsx | ||
| import { | ||
| useChat, | ||
| fetchServerSentEvents, | ||
| StreamHttpError, | ||
| } from "@tanstack/ai-react"; | ||
|
|
||
| export function ChatRateLimit() { | ||
| const { error } = useChat({ | ||
| connection: fetchServerSentEvents("/api/chat"), | ||
| threadId: "support-chat", | ||
| onError: (err) => { | ||
| if (err instanceof StreamHttpError && err.status === 429) { | ||
| console.warn("Rate limited. Retry in", err.retryAfterMs, "ms"); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| return error ? <p>{error.message}</p> : null; | ||
| } | ||
| ``` | ||
|
|
||
| - `status`: the HTTP status code. | ||
| - `retryAfterMs`: the `Retry-After` delay in milliseconds. It is `undefined` when the response has no valid `Retry-After` header. | ||
|
|
||
| The `error` state holds a plain `Error` with the same message. Do the `instanceof` check in `onError`. | ||
|
|
||
| You can also import `StreamHttpError` from `@tanstack/ai-client`. | ||
|
|
||
| In a custom transport, do not catch and hide an `AbortError`. Let it propagate, so that the client knows that the abort worked. | ||
|
|
||
| ### Rules for a custom transport | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document the BYOK exception to non-2xx handling.
When a 401 response has the BYOK-missing body, both fetch and XHR throw
ByokMissingErrorinstead. Aninstanceof StreamHttpErrorcheck therefore does not match that response. Please note this exception here and in the client API description.