-
Notifications
You must be signed in to change notification settings - Fork 317
Introduce a new error type to represent AbortError #357
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b5c2666
add a new error to represent AbortError from SDK
hkt74 1e4f870
changesets
hkt74 4f0a6a1
format
hkt74 cd34b29
fix imports order
hkt74 d00e6eb
update docs
hkt74 42a1e3a
Revert "update docs"
hkt74 0be5f7f
api docs
hkt74 2ffdd26
catch AbortError from stream reader
hkt74 a369461
format
hkt74 94bcc9d
test
hkt74 4aa69d9
fix import
hkt74 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@google/generative-ai": minor | ||
| --- | ||
|
|
||
| Introduce a new error type to represent AbortError from SDK |
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
15 changes: 15 additions & 0 deletions
15
docs/reference/main/generative-ai.googlegenerativeaiaborterror.md
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,15 @@ | ||
| <!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
|
||
| [Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIAbortError](./generative-ai.googlegenerativeaiaborterror.md) | ||
|
|
||
| ## GoogleGenerativeAIAbortError class | ||
|
|
||
| Error thrown when a request is aborted, either due to a timeout or intentional cancellation by the user. | ||
|
|
||
| **Signature:** | ||
|
|
||
| ```typescript | ||
| export declare class GoogleGenerativeAIAbortError extends GoogleGenerativeAIError | ||
| ``` | ||
| **Extends:** [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) | ||
|
|
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
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 |
|---|---|---|
|
|
@@ -22,7 +22,10 @@ import { | |
| GenerateContentStreamResult, | ||
| Part, | ||
| } from "../../types"; | ||
| import { GoogleGenerativeAIError } from "../errors"; | ||
| import { | ||
| GoogleGenerativeAIAbortError, | ||
| GoogleGenerativeAIError, | ||
| } from "../errors"; | ||
| import { addHelpers } from "./response-helpers"; | ||
|
|
||
| const responseLineRE = /^data\: (.*)(?:\n\n|\r\r|\r\n\r\n)/; | ||
|
|
@@ -89,38 +92,54 @@ export function getResponseStream<T>( | |
| let currentText = ""; | ||
| return pump(); | ||
| function pump(): Promise<(() => Promise<void>) | undefined> { | ||
| return reader.read().then(({ value, done }) => { | ||
| if (done) { | ||
| if (currentText.trim()) { | ||
| controller.error( | ||
| new GoogleGenerativeAIError("Failed to parse stream"), | ||
| ); | ||
| return reader | ||
| .read() | ||
| .then(({ value, done }) => { | ||
| if (done) { | ||
| if (currentText.trim()) { | ||
| controller.error( | ||
| new GoogleGenerativeAIError("Failed to parse stream"), | ||
| ); | ||
| return; | ||
| } | ||
| controller.close(); | ||
| return; | ||
| } | ||
| controller.close(); | ||
| return; | ||
| } | ||
|
|
||
| currentText += value; | ||
| let match = currentText.match(responseLineRE); | ||
| let parsedResponse: T; | ||
| while (match) { | ||
| try { | ||
| parsedResponse = JSON.parse(match[1]); | ||
| } catch (e) { | ||
| controller.error( | ||
| new GoogleGenerativeAIError( | ||
| `Error parsing JSON response: "${match[1]}"`, | ||
| ), | ||
| currentText += value; | ||
| let match = currentText.match(responseLineRE); | ||
| let parsedResponse: T; | ||
| while (match) { | ||
| try { | ||
| parsedResponse = JSON.parse(match[1]); | ||
| } catch (e) { | ||
| controller.error( | ||
| new GoogleGenerativeAIError( | ||
| `Error parsing JSON response: "${match[1]}"`, | ||
| ), | ||
| ); | ||
| return; | ||
| } | ||
| controller.enqueue(parsedResponse); | ||
| currentText = currentText.substring(match[0].length); | ||
| match = currentText.match(responseLineRE); | ||
| } | ||
| return pump(); | ||
| }) | ||
| .catch((e: Error) => { | ||
| let err = e; | ||
| err.stack = e.stack; | ||
| if (err.name === "AbortError") { | ||
| err = new GoogleGenerativeAIAbortError( | ||
| "Request aborted when reading from the stream", | ||
| ); | ||
| } else { | ||
| err = new GoogleGenerativeAIError( | ||
| "Error reading from the stream", | ||
| ); | ||
|
Collaborator
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. Could we just do throw new GoogleGenerativeAIError('Failed to perform operation', { cause: error }); Error supports cause, and I think it negates the need for specific classes. |
||
| return; | ||
| } | ||
| controller.enqueue(parsedResponse); | ||
| currentText = currentText.substring(match[0].length); | ||
| match = currentText.match(responseLineRE); | ||
| } | ||
| return pump(); | ||
| }); | ||
| throw err; | ||
| }); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
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
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.