-
Notifications
You must be signed in to change notification settings - Fork 90
fix(streaming): preserve provider errors through abort cleanup #322
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
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { APICallError } from "@ai-sdk/provider"; | ||
| import { describe, expect, test } from "vitest"; | ||
| import { errorToString } from "./errors.js"; | ||
|
|
||
| describe("errorToString", () => { | ||
| test("preserves provider error classifications", () => { | ||
| const details = { | ||
| error: { | ||
| code: "invalid_prompt", | ||
| message: "Invalid prompt: flagged by policy", | ||
| }, | ||
| }; | ||
| const apiError = new APICallError({ | ||
| message: "Invalid prompt: flagged by policy", | ||
| url: "https://api.example.test", | ||
| requestBodyValues: {}, | ||
| statusCode: 400, | ||
| data: details, | ||
| }); | ||
|
|
||
| expect(errorToString(details)).toBe( | ||
| "invalid_prompt: Invalid prompt: flagged by policy", | ||
| ); | ||
| expect(errorToString(apiError)).toBe( | ||
| "invalid_prompt: Invalid prompt: flagged by policy", | ||
| ); | ||
| expect(errorToString(new Error())).toBe("Error"); | ||
| expect(errorToString(new TypeError())).toBe("TypeError"); | ||
| const systemError = Object.assign(new Error("socket hang up"), { | ||
| code: "ECONNRESET", | ||
| }); | ||
| expect(errorToString(systemError)).toBe("socket hang up"); | ||
| const codeOnly = Object.assign(new Error("Request failed"), { | ||
| data: { code: "rate_limit" }, | ||
| }); | ||
| expect(errorToString(codeOnly)).toBe("rate_limit: Request failed"); | ||
| }); | ||
|
|
||
| test("serializes objects without mistaking shared values for cycles", () => { | ||
| const shared = { detail: "provider disconnected" }; | ||
| const circular: Record<string, unknown> = { shared }; | ||
| circular.self = circular; | ||
|
|
||
| expect(errorToString({ x: shared, y: shared })).toBe( | ||
| '{"x":{"detail":"provider disconnected"},"y":{"detail":"provider disconnected"}}', | ||
| ); | ||
| expect(errorToString(circular)).toBe( | ||
| '{"shared":{"detail":"provider disconnected"},"self":"[Circular]"}', | ||
| ); | ||
| }); | ||
|
|
||
| test("bounds stored error text without splitting surrogate pairs", () => { | ||
| const serialized = errorToString(`${"x".repeat(1022)}😀tail`); | ||
|
|
||
| expect(serialized.length).toBeLessThanOrEqual(1024); | ||
| expect(serialized.endsWith("x…")).toBe(true); | ||
| }); | ||
|
|
||
| test("does not throw when Error properties are hostile accessors", () => { | ||
| const error = new Error(); | ||
| Object.defineProperties(error, { | ||
| message: { | ||
| get() { | ||
| throw new Error("message getter failed"); | ||
| }, | ||
| }, | ||
| name: { | ||
| get() { | ||
| throw new Error("name getter failed"); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expect(errorToString(error)).toBe("Unknown error"); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| const MAX_ERROR_LENGTH = 1024; | ||
|
|
||
| export function errorToString(error: unknown): string { | ||
| return truncateError(describeError(error)); | ||
| } | ||
|
|
||
| function describeError(error: unknown): string { | ||
| if (typeof error === "string") return error; | ||
| if (error instanceof Error) { | ||
| const message = property(error, "message"); | ||
| if (typeof message !== "string" || message.length === 0) { | ||
| const name = property(error, "name"); | ||
| return typeof name === "string" && name.length > 0 | ||
| ? name | ||
| : safeString(error); | ||
| } | ||
| const nested = errorDetails( | ||
| property(error, "error") ?? property(error, "data"), | ||
| ); | ||
| return ( | ||
| formatDetails({ | ||
| message: nested.message ?? message, | ||
| code: nested.code, | ||
| }) ?? message | ||
| ); | ||
| } | ||
|
|
||
| const details = formatDetails(errorDetails(error)); | ||
| if (details) return details; | ||
|
|
||
| if (error && typeof error === "object") { | ||
| try { | ||
| const ancestors: object[] = []; | ||
| const serialized = JSON.stringify(error, function (_key, value: unknown) { | ||
| if (typeof value === "bigint") return value.toString(); | ||
| if (!value || typeof value !== "object") return value; | ||
| while (ancestors.length > 0 && ancestors.at(-1) !== this) { | ||
| ancestors.pop(); | ||
| } | ||
| if (ancestors.includes(value)) return "[Circular]"; | ||
| ancestors.push(value); | ||
| return value; | ||
| }); | ||
| if (serialized) return serialized; | ||
| } catch { | ||
| return safeString(error); | ||
| } | ||
| } | ||
|
|
||
| return safeString(error); | ||
| } | ||
|
|
||
| function safeString(error: unknown): string { | ||
| try { | ||
| return String(error); | ||
| } catch { | ||
| return "Unknown error"; | ||
| } | ||
| } | ||
|
|
||
| function errorDetails(error: unknown): { message?: string; code?: string } { | ||
| let current = error; | ||
| let message: string | undefined; | ||
| let code: string | undefined; | ||
| for (let depth = 0; depth < 3; depth++) { | ||
| if (typeof current === "string") { | ||
| message ??= current; | ||
| break; | ||
| } | ||
| if (!current || typeof current !== "object") break; | ||
|
|
||
| const currentMessage = property(current, "message"); | ||
| if (typeof currentMessage === "string" && currentMessage.length > 0) { | ||
| message ??= currentMessage; | ||
| } | ||
| const currentCode = property(current, "code"); | ||
| if (typeof currentCode === "string" || typeof currentCode === "number") { | ||
| code ??= String(currentCode); | ||
| } | ||
| if (message && code) break; | ||
| current = property(current, "error") ?? property(current, "data"); | ||
| } | ||
| return { message, code }; | ||
| } | ||
|
|
||
| function property(value: object, key: string): unknown { | ||
| try { | ||
| return (value as Record<string, unknown>)[key]; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| function formatDetails({ | ||
| message, | ||
| code, | ||
| }: { | ||
| message?: string; | ||
| code?: string; | ||
| }): string | undefined { | ||
| if (message && code) { | ||
| return message.startsWith(`${code}:`) ? message : `${code}: ${message}`; | ||
| } | ||
| return message ?? code; | ||
| } | ||
|
|
||
| function truncateError(error: string): string { | ||
| if (error.length <= MAX_ERROR_LENGTH) return error; | ||
| let truncated = error.slice(0, MAX_ERROR_LENGTH - 1); | ||
| const last = truncated.charCodeAt(truncated.length - 1); | ||
| if (last >= 0xd800 && last <= 0xdbff) truncated = truncated.slice(0, -1); | ||
| return `${truncated}…`; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.