From 58217c7f9bc0d03e2280032f7bee49f9878deaee Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Fri, 26 Apr 2024 09:29:59 -0700 Subject: [PATCH 1/4] Add more properties to errors and export errors. --- .changeset/quick-actors-beam.md | 5 ++ ...i.googlegenerativeaierror._constructor_.md | 20 +++++ .../generative-ai.googlegenerativeaierror.md | 27 ++++++ ...glegenerativeaifetcherror._constructor_.md | 23 ++++++ ...oglegenerativeaifetcherror.errordetails.md | 11 +++ ...erative-ai.googlegenerativeaifetcherror.md | 27 ++++++ ...-ai.googlegenerativeaifetcherror.status.md | 11 +++ ...googlegenerativeaifetcherror.statustext.md | 11 +++ ...generativeairesponseerror._constructor_.md | 21 +++++ ...tive-ai.googlegenerativeairesponseerror.md | 25 ++++++ ...ooglegenerativeairesponseerror.response.md | 11 +++ docs/reference/main/generative-ai.md | 3 + packages/main/src/errors.ts | 20 +++++ packages/main/src/files/request.test.ts | 82 +++++++++++++++---- packages/main/src/files/request.ts | 29 +++++-- packages/main/src/index.ts | 1 + packages/main/src/requests/request.test.ts | 74 +++++++++++++---- packages/main/src/requests/request.ts | 27 ++++-- 18 files changed, 382 insertions(+), 46 deletions(-) create mode 100644 .changeset/quick-actors-beam.md create mode 100644 docs/reference/main/generative-ai.googlegenerativeaierror._constructor_.md create mode 100644 docs/reference/main/generative-ai.googlegenerativeaierror.md create mode 100644 docs/reference/main/generative-ai.googlegenerativeaifetcherror._constructor_.md create mode 100644 docs/reference/main/generative-ai.googlegenerativeaifetcherror.errordetails.md create mode 100644 docs/reference/main/generative-ai.googlegenerativeaifetcherror.md create mode 100644 docs/reference/main/generative-ai.googlegenerativeaifetcherror.status.md create mode 100644 docs/reference/main/generative-ai.googlegenerativeaifetcherror.statustext.md create mode 100644 docs/reference/main/generative-ai.googlegenerativeairesponseerror._constructor_.md create mode 100644 docs/reference/main/generative-ai.googlegenerativeairesponseerror.md create mode 100644 docs/reference/main/generative-ai.googlegenerativeairesponseerror.response.md diff --git a/.changeset/quick-actors-beam.md b/.changeset/quick-actors-beam.md new file mode 100644 index 00000000..d304e9e4 --- /dev/null +++ b/.changeset/quick-actors-beam.md @@ -0,0 +1,5 @@ +--- +"@google/generative-ai": minor +--- + +Export error classes and add more properties to fetch errors. diff --git a/docs/reference/main/generative-ai.googlegenerativeaierror._constructor_.md b/docs/reference/main/generative-ai.googlegenerativeaierror._constructor_.md new file mode 100644 index 00000000..1addcdd3 --- /dev/null +++ b/docs/reference/main/generative-ai.googlegenerativeaierror._constructor_.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) > [(constructor)](./generative-ai.googlegenerativeaierror._constructor_.md) + +## GoogleGenerativeAIError.(constructor) + +Constructs a new instance of the `GoogleGenerativeAIError` class + +**Signature:** + +```typescript +constructor(message: string); +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| message | string | | + diff --git a/docs/reference/main/generative-ai.googlegenerativeaierror.md b/docs/reference/main/generative-ai.googlegenerativeaierror.md new file mode 100644 index 00000000..77035398 --- /dev/null +++ b/docs/reference/main/generative-ai.googlegenerativeaierror.md @@ -0,0 +1,27 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) + +## GoogleGenerativeAIError class + + Copyright 2024 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + +**Signature:** + +```typescript +export declare class GoogleGenerativeAIError extends Error +``` +**Extends:** Error + +## Constructors + +| Constructor | Modifiers | Description | +| --- | --- | --- | +| [(constructor)(message)](./generative-ai.googlegenerativeaierror._constructor_.md) | | Constructs a new instance of the GoogleGenerativeAIError class | + diff --git a/docs/reference/main/generative-ai.googlegenerativeaifetcherror._constructor_.md b/docs/reference/main/generative-ai.googlegenerativeaifetcherror._constructor_.md new file mode 100644 index 00000000..76217150 --- /dev/null +++ b/docs/reference/main/generative-ai.googlegenerativeaifetcherror._constructor_.md @@ -0,0 +1,23 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) > [(constructor)](./generative-ai.googlegenerativeaifetcherror._constructor_.md) + +## GoogleGenerativeAIFetchError.(constructor) + +Constructs a new instance of the `GoogleGenerativeAIFetchError` class + +**Signature:** + +```typescript +constructor(message: string, status?: number, statusText?: string, errorDetails?: ErrorDetails[]); +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| message | string | | +| status | number | _(Optional)_ | +| statusText | string | _(Optional)_ | +| errorDetails | ErrorDetails\[\] | _(Optional)_ | + diff --git a/docs/reference/main/generative-ai.googlegenerativeaifetcherror.errordetails.md b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.errordetails.md new file mode 100644 index 00000000..6bc20ff1 --- /dev/null +++ b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.errordetails.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) > [errorDetails](./generative-ai.googlegenerativeaifetcherror.errordetails.md) + +## GoogleGenerativeAIFetchError.errorDetails property + +**Signature:** + +```typescript +errorDetails?: ErrorDetails[]; +``` diff --git a/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md new file mode 100644 index 00000000..470b2c6c --- /dev/null +++ b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md @@ -0,0 +1,27 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) + +## GoogleGenerativeAIFetchError class + +**Signature:** + +```typescript +export declare class GoogleGenerativeAIFetchError extends GoogleGenerativeAIError +``` +**Extends:** [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) + +## Constructors + +| Constructor | Modifiers | Description | +| --- | --- | --- | +| [(constructor)(message, status, statusText, errorDetails)](./generative-ai.googlegenerativeaifetcherror._constructor_.md) | | Constructs a new instance of the GoogleGenerativeAIFetchError class | + +## Properties + +| Property | Modifiers | Type | Description | +| --- | --- | --- | --- | +| [errorDetails?](./generative-ai.googlegenerativeaifetcherror.errordetails.md) | | ErrorDetails\[\] | _(Optional)_ | +| [status?](./generative-ai.googlegenerativeaifetcherror.status.md) | | number | _(Optional)_ | +| [statusText?](./generative-ai.googlegenerativeaifetcherror.statustext.md) | | string | _(Optional)_ | + diff --git a/docs/reference/main/generative-ai.googlegenerativeaifetcherror.status.md b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.status.md new file mode 100644 index 00000000..ccad093a --- /dev/null +++ b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.status.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) > [status](./generative-ai.googlegenerativeaifetcherror.status.md) + +## GoogleGenerativeAIFetchError.status property + +**Signature:** + +```typescript +status?: number; +``` diff --git a/docs/reference/main/generative-ai.googlegenerativeaifetcherror.statustext.md b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.statustext.md new file mode 100644 index 00000000..923a927d --- /dev/null +++ b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.statustext.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) > [statusText](./generative-ai.googlegenerativeaifetcherror.statustext.md) + +## GoogleGenerativeAIFetchError.statusText property + +**Signature:** + +```typescript +statusText?: string; +``` diff --git a/docs/reference/main/generative-ai.googlegenerativeairesponseerror._constructor_.md b/docs/reference/main/generative-ai.googlegenerativeairesponseerror._constructor_.md new file mode 100644 index 00000000..019c6073 --- /dev/null +++ b/docs/reference/main/generative-ai.googlegenerativeairesponseerror._constructor_.md @@ -0,0 +1,21 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) > [(constructor)](./generative-ai.googlegenerativeairesponseerror._constructor_.md) + +## GoogleGenerativeAIResponseError.(constructor) + +Constructs a new instance of the `GoogleGenerativeAIResponseError` class + +**Signature:** + +```typescript +constructor(message: string, response?: T); +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| message | string | | +| response | T | _(Optional)_ | + diff --git a/docs/reference/main/generative-ai.googlegenerativeairesponseerror.md b/docs/reference/main/generative-ai.googlegenerativeairesponseerror.md new file mode 100644 index 00000000..a4e4c556 --- /dev/null +++ b/docs/reference/main/generative-ai.googlegenerativeairesponseerror.md @@ -0,0 +1,25 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) + +## GoogleGenerativeAIResponseError class + +**Signature:** + +```typescript +export declare class GoogleGenerativeAIResponseError extends GoogleGenerativeAIError +``` +**Extends:** [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) + +## Constructors + +| Constructor | Modifiers | Description | +| --- | --- | --- | +| [(constructor)(message, response)](./generative-ai.googlegenerativeairesponseerror._constructor_.md) | | Constructs a new instance of the GoogleGenerativeAIResponseError class | + +## Properties + +| Property | Modifiers | Type | Description | +| --- | --- | --- | --- | +| [response?](./generative-ai.googlegenerativeairesponseerror.response.md) | | T | _(Optional)_ | + diff --git a/docs/reference/main/generative-ai.googlegenerativeairesponseerror.response.md b/docs/reference/main/generative-ai.googlegenerativeairesponseerror.response.md new file mode 100644 index 00000000..335abdcf --- /dev/null +++ b/docs/reference/main/generative-ai.googlegenerativeairesponseerror.response.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) > [response](./generative-ai.googlegenerativeairesponseerror.response.md) + +## GoogleGenerativeAIResponseError.response property + +**Signature:** + +```typescript +response?: T; +``` diff --git a/docs/reference/main/generative-ai.md b/docs/reference/main/generative-ai.md index a231bee1..167c4ad7 100644 --- a/docs/reference/main/generative-ai.md +++ b/docs/reference/main/generative-ai.md @@ -11,6 +11,9 @@ | [ChatSession](./generative-ai.chatsession.md) | ChatSession class that enables sending chat messages and stores history of sent and received messages so far. | | [GenerativeModel](./generative-ai.generativemodel.md) | Class for generative model APIs. | | [GoogleGenerativeAI](./generative-ai.googlegenerativeai.md) | Top-level class for this SDK | +| [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) |

Copyright 2024 Google LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

| +| [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) | | +| [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) | | ## Enumerations diff --git a/packages/main/src/errors.ts b/packages/main/src/errors.ts index 795049de..1bdccb4b 100644 --- a/packages/main/src/errors.ts +++ b/packages/main/src/errors.ts @@ -31,3 +31,23 @@ export class GoogleGenerativeAIResponseError< super(message); } } + +export class GoogleGenerativeAIFetchError extends GoogleGenerativeAIError { + constructor( + message: string, + public status?: number, + public statusText?: string, + public errorDetails?: ErrorDetails[], + ) { + super(message); + } +} + +interface ErrorDetails { + "@type"?: string; + reason?: string; + domain?: string; + metadata?: Record; + details?: Record; + [key: string]: unknown; +} diff --git a/packages/main/src/files/request.test.ts b/packages/main/src/files/request.test.ts index 225f8676..ad92dda7 100644 --- a/packages/main/src/files/request.test.ts +++ b/packages/main/src/files/request.test.ts @@ -22,6 +22,7 @@ import * as chaiAsPromised from "chai-as-promised"; import { DEFAULT_API_VERSION, DEFAULT_BASE_URL } from "../requests/request"; import { FilesRequestUrl, makeFilesRequest } from "./request"; import { FilesTask } from "./constants"; +import { GoogleGenerativeAIFetchError } from "../errors"; use(sinonChai); use(chaiAsPromised); @@ -97,9 +98,22 @@ describe("Files API - request methods", () => { const url = new FilesRequestUrl(FilesTask.GET, "key", { timeout: 0 }); const headers = new Headers(); - await expect( - makeFilesRequest(url, headers, new Blob(), fetchStub as typeof fetch), - ).to.be.rejectedWith("500 AbortError"); + try { + await makeFilesRequest( + url, + headers, + new Blob(), + fetchStub as typeof fetch, + ); + } catch (e) { + expect((e as GoogleGenerativeAIFetchError).message).to.include( + "500 AbortError", + ); + expect((e as GoogleGenerativeAIFetchError).status).to.equal(500); + expect((e as GoogleGenerativeAIFetchError).statusText).to.equal( + "AbortError", + ); + } expect(fetchStub).to.be.calledOnce; }); it("Network error, no response.json()", async () => { @@ -110,9 +124,22 @@ describe("Files API - request methods", () => { } as Response); const url = new FilesRequestUrl(FilesTask.GET, "key"); const headers = new Headers(); - await expect( - makeFilesRequest(url, headers, new Blob(), fetchStub as typeof fetch), - ).to.be.rejectedWith(/500 Server Error/); + try { + await makeFilesRequest( + url, + headers, + new Blob(), + fetchStub as typeof fetch, + ); + } catch (e) { + expect((e as GoogleGenerativeAIFetchError).message).to.include( + "500 Server Error", + ); + expect((e as GoogleGenerativeAIFetchError).status).to.equal(500); + expect((e as GoogleGenerativeAIFetchError).statusText).to.equal( + "Server Error", + ); + } expect(fetchStub).to.be.calledOnce; }); it("Network error, includes response.json()", async () => { @@ -124,9 +151,22 @@ describe("Files API - request methods", () => { } as Response); const url = new FilesRequestUrl(FilesTask.GET, "key"); const headers = new Headers(); - await expect( - makeFilesRequest(url, headers, new Blob(), fetchStub as typeof fetch), - ).to.be.rejectedWith(/500 Server Error.*extra info/); + try { + await makeFilesRequest( + url, + headers, + new Blob(), + fetchStub as typeof fetch, + ); + } catch (e) { + expect((e as GoogleGenerativeAIFetchError).message).to.match( + /500 Server Error.+extra info/, + ); + expect((e as GoogleGenerativeAIFetchError).status).to.equal(500); + expect((e as GoogleGenerativeAIFetchError).statusText).to.equal( + "Server Error", + ); + } expect(fetchStub).to.be.calledOnce; }); it("Network error, includes response.json() and details", async () => { @@ -150,11 +190,25 @@ describe("Files API - request methods", () => { } as Response); const url = new FilesRequestUrl(FilesTask.GET, "key"); const headers = new Headers(); - await expect( - makeFilesRequest(url, headers, new Blob(), fetchStub as typeof fetch), - ).to.be.rejectedWith( - /500 Server Error.*extra info.*generic::invalid_argument/, - ); + try { + await makeFilesRequest( + url, + headers, + new Blob(), + fetchStub as typeof fetch, + ); + } catch (e) { + expect((e as GoogleGenerativeAIFetchError).message).to.match( + /500 Server Error.*extra info.*generic::invalid_argument/, + ); + expect((e as GoogleGenerativeAIFetchError).status).to.equal(500); + expect((e as GoogleGenerativeAIFetchError).statusText).to.equal( + "Server Error", + ); + expect( + (e as GoogleGenerativeAIFetchError).errorDetails[0].detail, + ).to.include("generic::invalid_argument"); + } expect(fetchStub).to.be.calledOnce; }); }); diff --git a/packages/main/src/files/request.ts b/packages/main/src/files/request.ts index e7b27ea1..489a1022 100644 --- a/packages/main/src/files/request.ts +++ b/packages/main/src/files/request.ts @@ -15,7 +15,10 @@ * limitations under the License. */ -import { GoogleGenerativeAIError } from "../errors"; +import { + GoogleGenerativeAIError, + GoogleGenerativeAIFetchError, +} from "../errors"; import { DEFAULT_API_VERSION, DEFAULT_BASE_URL, @@ -93,26 +96,36 @@ export async function makeFilesRequest( const response = await fetchFn(url.toString(), requestInit); if (!response.ok) { let message = ""; + let errorDetails; try { const json = await response.json(); message = json.error.message; if (json.error.details) { message += ` ${JSON.stringify(json.error.details)}`; + errorDetails = json.error.details; } } catch (e) { // ignored } - throw new Error(`[${response.status} ${response.statusText}] ${message}`); + throw new GoogleGenerativeAIFetchError( + `Error fetching from ${url.toString()}: [${response.status} ${ + response.statusText + }] ${message}`, + response.status, + response.statusText, + errorDetails, + ); } else { return response; } } catch (e) { - const err = new GoogleGenerativeAIError( - `Error on task type: ${url.task} fetching from ${url.toString()}: ${ - e.message - }`, - ); - err.stack = e.stack; + let err = e; + if (!(e instanceof GoogleGenerativeAIFetchError)) { + err = new GoogleGenerativeAIError( + `Error fetching from ${url.toString()}: ${e.message}`, + ); + err.stack = e.stack; + } throw err; } } diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index 903ccb2b..ec9db61a 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts @@ -17,3 +17,4 @@ export * from "../types"; export * from "./gen-ai"; +export * from "./errors"; diff --git a/packages/main/src/requests/request.test.ts b/packages/main/src/requests/request.test.ts index 288f1d9a..6f02f6cb 100644 --- a/packages/main/src/requests/request.test.ts +++ b/packages/main/src/requests/request.test.ts @@ -27,6 +27,7 @@ import { _makeRequestInternal, constructRequest, } from "./request"; +import { GoogleGenerativeAIFetchError } from "../errors"; use(sinonChai); use(chaiAsPromised); @@ -183,8 +184,8 @@ describe("request methods", () => { statusText: "AbortError", } as Response); - await expect( - _makeRequestInternal( + try { + await _makeRequestInternal( "model-name", Task.GENERATE_CONTENT, "key", @@ -194,8 +195,16 @@ describe("request methods", () => { timeout: 0, }, fetchStub as typeof fetch, - ), - ).to.be.rejectedWith("500 AbortError"); + ); + } catch (e) { + expect((e as GoogleGenerativeAIFetchError).status).to.equal(500); + expect((e as GoogleGenerativeAIFetchError).statusText).to.equal( + "AbortError", + ); + expect((e as GoogleGenerativeAIFetchError).message).to.include( + "500 AbortError", + ); + } expect(fetchStub).to.be.calledOnce; }); it("Network error, no response.json()", async () => { @@ -204,8 +213,8 @@ describe("request methods", () => { status: 500, statusText: "Server Error", } as Response); - await expect( - _makeRequestInternal( + try { + await _makeRequestInternal( "model-name", Task.GENERATE_CONTENT, "key", @@ -213,8 +222,16 @@ describe("request methods", () => { "", {}, fetchStub as typeof fetch, - ), - ).to.be.rejectedWith(/500 Server Error/); + ); + } catch (e) { + expect((e as GoogleGenerativeAIFetchError).status).to.equal(500); + expect((e as GoogleGenerativeAIFetchError).statusText).to.equal( + "Server Error", + ); + expect((e as GoogleGenerativeAIFetchError).message).to.include( + "500 Server Error", + ); + } expect(fetchStub).to.be.calledOnce; }); it("Network error, includes response.json()", async () => { @@ -224,8 +241,9 @@ describe("request methods", () => { statusText: "Server Error", json: () => Promise.resolve({ error: { message: "extra info" } }), } as Response); - await expect( - _makeRequestInternal( + + try { + await _makeRequestInternal( "model-name", Task.GENERATE_CONTENT, "key", @@ -233,8 +251,16 @@ describe("request methods", () => { "", {}, fetchStub as typeof fetch, - ), - ).to.be.rejectedWith(/500 Server Error.*extra info/); + ); + } catch (e) { + expect((e as GoogleGenerativeAIFetchError).status).to.equal(500); + expect((e as GoogleGenerativeAIFetchError).statusText).to.equal( + "Server Error", + ); + expect((e as GoogleGenerativeAIFetchError).message).to.match( + /500 Server Error.*extra info/, + ); + } expect(fetchStub).to.be.calledOnce; }); it("Network error, includes response.json() and details", async () => { @@ -256,8 +282,9 @@ describe("request methods", () => { }, }), } as Response); - await expect( - _makeRequestInternal( + + try { + await _makeRequestInternal( "model-name", Task.GENERATE_CONTENT, "key", @@ -265,10 +292,21 @@ describe("request methods", () => { "", {}, fetchStub as typeof fetch, - ), - ).to.be.rejectedWith( - /500 Server Error.*extra info.*generic::invalid_argument/, - ); + ); + } catch (e) { + expect((e as GoogleGenerativeAIFetchError).status).to.equal(500); + expect((e as GoogleGenerativeAIFetchError).statusText).to.equal( + "Server Error", + ); + expect( + (e as GoogleGenerativeAIFetchError).errorDetails[0].detail, + ).to.equal( + "[ORIGINAL ERROR] generic::invalid_argument: invalid status photos.thumbnailer.Status.Code::5: Source image 0 too short", + ); + expect((e as GoogleGenerativeAIFetchError).message).to.match( + /500 Server Error.*extra info.*generic::invalid_argument/, + ); + } expect(fetchStub).to.be.calledOnce; }); }); diff --git a/packages/main/src/requests/request.ts b/packages/main/src/requests/request.ts index dac7ce33..2485ef99 100644 --- a/packages/main/src/requests/request.ts +++ b/packages/main/src/requests/request.ts @@ -16,7 +16,10 @@ */ import { RequestOptions } from "../../types"; -import { GoogleGenerativeAIError } from "../errors"; +import { + GoogleGenerativeAIError, + GoogleGenerativeAIFetchError, +} from "../errors"; export const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com"; @@ -143,22 +146,34 @@ export async function _makeRequestInternal( response = await fetchFn(request.url, request.fetchOptions); if (!response.ok) { let message = ""; + let errorDetails; try { const json = await response.json(); message = json.error.message; if (json.error.details) { message += ` ${JSON.stringify(json.error.details)}`; + errorDetails = json.error.details; } } catch (e) { // ignored } - throw new Error(`[${response.status} ${response.statusText}] ${message}`); + throw new GoogleGenerativeAIFetchError( + `Error fetching from ${url.toString()}: [${response.status} ${ + response.statusText + }] ${message}`, + response.status, + response.statusText, + errorDetails, + ); } } catch (e) { - const err = new GoogleGenerativeAIError( - `Error fetching from ${url.toString()}: ${e.message}`, - ); - err.stack = e.stack; + let err = e; + if (!(e instanceof GoogleGenerativeAIFetchError)) { + err = new GoogleGenerativeAIError( + `Error fetching from ${url.toString()}: ${e.message}`, + ); + err.stack = e.stack; + } throw err; } return response; From 781bbd138180713c4eb49edeb6cba6e6aa3e8740 Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Fri, 26 Apr 2024 09:33:12 -0700 Subject: [PATCH 2/4] remove recursive details field --- packages/main/src/errors.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/main/src/errors.ts b/packages/main/src/errors.ts index 1bdccb4b..6aa5c5a9 100644 --- a/packages/main/src/errors.ts +++ b/packages/main/src/errors.ts @@ -48,6 +48,5 @@ interface ErrorDetails { reason?: string; domain?: string; metadata?: Record; - details?: Record; [key: string]: unknown; } From f7b520d46f98ae383148dddd2840eb288630c3aa Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Fri, 26 Apr 2024 09:38:43 -0700 Subject: [PATCH 3/4] add doc comments --- .../generative-ai.googlegenerativeaierror.md | 8 +------- ...enerative-ai.googlegenerativeaifetcherror.md | 2 ++ ...rative-ai.googlegenerativeairesponseerror.md | 2 ++ docs/reference/main/generative-ai.md | 6 +++--- packages/main/src/errors.ts | 17 +++++++++++++++++ 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/docs/reference/main/generative-ai.googlegenerativeaierror.md b/docs/reference/main/generative-ai.googlegenerativeaierror.md index 77035398..8e1da55c 100644 --- a/docs/reference/main/generative-ai.googlegenerativeaierror.md +++ b/docs/reference/main/generative-ai.googlegenerativeaierror.md @@ -4,13 +4,7 @@ ## GoogleGenerativeAIError class - Copyright 2024 Google LLC - -Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +Basic error type for this SDK. **Signature:** diff --git a/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md index 470b2c6c..d2603be4 100644 --- a/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md +++ b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md @@ -4,6 +4,8 @@ ## GoogleGenerativeAIFetchError class +Error class covering http errors when calling the server. Includes http status, statusText, and optional details, if provided in the server response. + **Signature:** ```typescript diff --git a/docs/reference/main/generative-ai.googlegenerativeairesponseerror.md b/docs/reference/main/generative-ai.googlegenerativeairesponseerror.md index a4e4c556..c6b628fc 100644 --- a/docs/reference/main/generative-ai.googlegenerativeairesponseerror.md +++ b/docs/reference/main/generative-ai.googlegenerativeairesponseerror.md @@ -4,6 +4,8 @@ ## GoogleGenerativeAIResponseError class +Errors in the contents of a response from the model. This includes parsing errors, or responses including a safety block reason. + **Signature:** ```typescript diff --git a/docs/reference/main/generative-ai.md b/docs/reference/main/generative-ai.md index 167c4ad7..8a480aa4 100644 --- a/docs/reference/main/generative-ai.md +++ b/docs/reference/main/generative-ai.md @@ -11,9 +11,9 @@ | [ChatSession](./generative-ai.chatsession.md) | ChatSession class that enables sending chat messages and stores history of sent and received messages so far. | | [GenerativeModel](./generative-ai.generativemodel.md) | Class for generative model APIs. | | [GoogleGenerativeAI](./generative-ai.googlegenerativeai.md) | Top-level class for this SDK | -| [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) |

Copyright 2024 Google LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

| -| [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) | | -| [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) | | +| [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) | Basic error type for this SDK. | +| [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) | Error class covering http errors when calling the server. Includes http status, statusText, and optional details, if provided in the server response. | +| [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) | Errors in the contents of a response from the model. This includes parsing errors, or responses including a safety block reason. | ## Enumerations diff --git a/packages/main/src/errors.ts b/packages/main/src/errors.ts index 6aa5c5a9..680896b5 100644 --- a/packages/main/src/errors.ts +++ b/packages/main/src/errors.ts @@ -15,12 +15,21 @@ * limitations under the License. */ +/** + * Basic error type for this SDK. + * @public + */ export class GoogleGenerativeAIError extends Error { constructor(message: string) { super(`[GoogleGenerativeAI Error]: ${message}`); } } +/** + * Errors in the contents of a response from the model. This includes parsing + * errors, or responses including a safety block reason. + * @public + */ export class GoogleGenerativeAIResponseError< T, > extends GoogleGenerativeAIError { @@ -32,6 +41,10 @@ export class GoogleGenerativeAIResponseError< } } +/** + * Error class covering http errors when calling the server. Includes http + * status, statusText, and optional details, if provided in the server response. + */ export class GoogleGenerativeAIFetchError extends GoogleGenerativeAIError { constructor( message: string, @@ -43,6 +56,10 @@ export class GoogleGenerativeAIFetchError extends GoogleGenerativeAIError { } } +/** + * Details object that may be included in an error response. + * @public + */ interface ErrorDetails { "@type"?: string; reason?: string; From 799819b7374e9ce1ebc7389a090b586075fcbc64 Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Mon, 29 Apr 2024 10:19:52 -0700 Subject: [PATCH 4/4] update text --- .../main/generative-ai.googlegenerativeaifetcherror.md | 2 +- docs/reference/main/generative-ai.md | 2 +- packages/main/src/errors.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md index d2603be4..6ff5c80d 100644 --- a/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md +++ b/docs/reference/main/generative-ai.googlegenerativeaifetcherror.md @@ -4,7 +4,7 @@ ## GoogleGenerativeAIFetchError class -Error class covering http errors when calling the server. Includes http status, statusText, and optional details, if provided in the server response. +Error class covering HTTP errors when calling the server. Includes HTTP status, statusText, and optional details, if provided in the server response. **Signature:** diff --git a/docs/reference/main/generative-ai.md b/docs/reference/main/generative-ai.md index 8a480aa4..2a31e0f2 100644 --- a/docs/reference/main/generative-ai.md +++ b/docs/reference/main/generative-ai.md @@ -12,7 +12,7 @@ | [GenerativeModel](./generative-ai.generativemodel.md) | Class for generative model APIs. | | [GoogleGenerativeAI](./generative-ai.googlegenerativeai.md) | Top-level class for this SDK | | [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) | Basic error type for this SDK. | -| [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) | Error class covering http errors when calling the server. Includes http status, statusText, and optional details, if provided in the server response. | +| [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) | Error class covering HTTP errors when calling the server. Includes HTTP status, statusText, and optional details, if provided in the server response. | | [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) | Errors in the contents of a response from the model. This includes parsing errors, or responses including a safety block reason. | ## Enumerations diff --git a/packages/main/src/errors.ts b/packages/main/src/errors.ts index 680896b5..4ab29a15 100644 --- a/packages/main/src/errors.ts +++ b/packages/main/src/errors.ts @@ -42,7 +42,7 @@ export class GoogleGenerativeAIResponseError< } /** - * Error class covering http errors when calling the server. Includes http + * Error class covering HTTP errors when calling the server. Includes HTTP * status, statusText, and optional details, if provided in the server response. */ export class GoogleGenerativeAIFetchError extends GoogleGenerativeAIError {