Skip to content

Commit 111e970

Browse files
authoredApr 29, 2024··
Add more properties to errors and export errors. (#112)
1 parent 7ef0500 commit 111e970

18 files changed

+396
-46
lines changed
 

‎.changeset/quick-actors-beam.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@google/generative-ai": minor
3+
---
4+
5+
Export error classes and add more properties to fetch errors.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) &gt; [(constructor)](./generative-ai.googlegenerativeaierror._constructor_.md)
4+
5+
## GoogleGenerativeAIError.(constructor)
6+
7+
Constructs a new instance of the `GoogleGenerativeAIError` class
8+
9+
**Signature:**
10+
11+
```typescript
12+
constructor(message: string);
13+
```
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| --- | --- | --- |
19+
| message | string | |
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md)
4+
5+
## GoogleGenerativeAIError class
6+
7+
Basic error type for this SDK.
8+
9+
**Signature:**
10+
11+
```typescript
12+
export declare class GoogleGenerativeAIError extends Error
13+
```
14+
**Extends:** Error
15+
16+
## Constructors
17+
18+
| Constructor | Modifiers | Description |
19+
| --- | --- | --- |
20+
| [(constructor)(message)](./generative-ai.googlegenerativeaierror._constructor_.md) | | Constructs a new instance of the <code>GoogleGenerativeAIError</code> class |
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) &gt; [(constructor)](./generative-ai.googlegenerativeaifetcherror._constructor_.md)
4+
5+
## GoogleGenerativeAIFetchError.(constructor)
6+
7+
Constructs a new instance of the `GoogleGenerativeAIFetchError` class
8+
9+
**Signature:**
10+
11+
```typescript
12+
constructor(message: string, status?: number, statusText?: string, errorDetails?: ErrorDetails[]);
13+
```
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| --- | --- | --- |
19+
| message | string | |
20+
| status | number | _(Optional)_ |
21+
| statusText | string | _(Optional)_ |
22+
| errorDetails | ErrorDetails\[\] | _(Optional)_ |
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) &gt; [errorDetails](./generative-ai.googlegenerativeaifetcherror.errordetails.md)
4+
5+
## GoogleGenerativeAIFetchError.errorDetails property
6+
7+
**Signature:**
8+
9+
```typescript
10+
errorDetails?: ErrorDetails[];
11+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md)
4+
5+
## GoogleGenerativeAIFetchError class
6+
7+
Error class covering HTTP errors when calling the server. Includes HTTP status, statusText, and optional details, if provided in the server response.
8+
9+
**Signature:**
10+
11+
```typescript
12+
export declare class GoogleGenerativeAIFetchError extends GoogleGenerativeAIError
13+
```
14+
**Extends:** [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md)
15+
16+
## Constructors
17+
18+
| Constructor | Modifiers | Description |
19+
| --- | --- | --- |
20+
| [(constructor)(message, status, statusText, errorDetails)](./generative-ai.googlegenerativeaifetcherror._constructor_.md) | | Constructs a new instance of the <code>GoogleGenerativeAIFetchError</code> class |
21+
22+
## Properties
23+
24+
| Property | Modifiers | Type | Description |
25+
| --- | --- | --- | --- |
26+
| [errorDetails?](./generative-ai.googlegenerativeaifetcherror.errordetails.md) | | ErrorDetails\[\] | _(Optional)_ |
27+
| [status?](./generative-ai.googlegenerativeaifetcherror.status.md) | | number | _(Optional)_ |
28+
| [statusText?](./generative-ai.googlegenerativeaifetcherror.statustext.md) | | string | _(Optional)_ |
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) &gt; [status](./generative-ai.googlegenerativeaifetcherror.status.md)
4+
5+
## GoogleGenerativeAIFetchError.status property
6+
7+
**Signature:**
8+
9+
```typescript
10+
status?: number;
11+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) &gt; [statusText](./generative-ai.googlegenerativeaifetcherror.statustext.md)
4+
5+
## GoogleGenerativeAIFetchError.statusText property
6+
7+
**Signature:**
8+
9+
```typescript
10+
statusText?: string;
11+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) &gt; [(constructor)](./generative-ai.googlegenerativeairesponseerror._constructor_.md)
4+
5+
## GoogleGenerativeAIResponseError.(constructor)
6+
7+
Constructs a new instance of the `GoogleGenerativeAIResponseError` class
8+
9+
**Signature:**
10+
11+
```typescript
12+
constructor(message: string, response?: T);
13+
```
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| --- | --- | --- |
19+
| message | string | |
20+
| response | T | _(Optional)_ |
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md)
4+
5+
## GoogleGenerativeAIResponseError class
6+
7+
Errors in the contents of a response from the model. This includes parsing errors, or responses including a safety block reason.
8+
9+
**Signature:**
10+
11+
```typescript
12+
export declare class GoogleGenerativeAIResponseError<T> extends GoogleGenerativeAIError
13+
```
14+
**Extends:** [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md)
15+
16+
## Constructors
17+
18+
| Constructor | Modifiers | Description |
19+
| --- | --- | --- |
20+
| [(constructor)(message, response)](./generative-ai.googlegenerativeairesponseerror._constructor_.md) | | Constructs a new instance of the <code>GoogleGenerativeAIResponseError</code> class |
21+
22+
## Properties
23+
24+
| Property | Modifiers | Type | Description |
25+
| --- | --- | --- | --- |
26+
| [response?](./generative-ai.googlegenerativeairesponseerror.response.md) | | T | _(Optional)_ |
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) &gt; [response](./generative-ai.googlegenerativeairesponseerror.response.md)
4+
5+
## GoogleGenerativeAIResponseError.response property
6+
7+
**Signature:**
8+
9+
```typescript
10+
response?: T;
11+
```

‎docs/reference/main/generative-ai.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
| [ChatSession](./generative-ai.chatsession.md) | ChatSession class that enables sending chat messages and stores history of sent and received messages so far. |
1212
| [GenerativeModel](./generative-ai.generativemodel.md) | Class for generative model APIs. |
1313
| [GoogleGenerativeAI](./generative-ai.googlegenerativeai.md) | Top-level class for this SDK |
14+
| [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) | Basic error type for this SDK. |
15+
| [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. |
16+
| [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. |
1417

1518
## Enumerations
1619

‎packages/main/src/errors.ts

+36
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@
1515
* limitations under the License.
1616
*/
1717

18+
/**
19+
* Basic error type for this SDK.
20+
* @public
21+
*/
1822
export class GoogleGenerativeAIError extends Error {
1923
constructor(message: string) {
2024
super(`[GoogleGenerativeAI Error]: ${message}`);
2125
}
2226
}
2327

28+
/**
29+
* Errors in the contents of a response from the model. This includes parsing
30+
* errors, or responses including a safety block reason.
31+
* @public
32+
*/
2433
export class GoogleGenerativeAIResponseError<
2534
T,
2635
> extends GoogleGenerativeAIError {
@@ -31,3 +40,30 @@ export class GoogleGenerativeAIResponseError<
3140
super(message);
3241
}
3342
}
43+
44+
/**
45+
* Error class covering HTTP errors when calling the server. Includes HTTP
46+
* status, statusText, and optional details, if provided in the server response.
47+
*/
48+
export class GoogleGenerativeAIFetchError extends GoogleGenerativeAIError {
49+
constructor(
50+
message: string,
51+
public status?: number,
52+
public statusText?: string,
53+
public errorDetails?: ErrorDetails[],
54+
) {
55+
super(message);
56+
}
57+
}
58+
59+
/**
60+
* Details object that may be included in an error response.
61+
* @public
62+
*/
63+
interface ErrorDetails {
64+
"@type"?: string;
65+
reason?: string;
66+
domain?: string;
67+
metadata?: Record<string, unknown>;
68+
[key: string]: unknown;
69+
}

‎packages/main/src/files/request.test.ts

+68-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as chaiAsPromised from "chai-as-promised";
2222
import { DEFAULT_API_VERSION, DEFAULT_BASE_URL } from "../requests/request";
2323
import { FilesRequestUrl, makeFilesRequest } from "./request";
2424
import { FilesTask } from "./constants";
25+
import { GoogleGenerativeAIFetchError } from "../errors";
2526

2627
use(sinonChai);
2728
use(chaiAsPromised);
@@ -97,9 +98,22 @@ describe("Files API - request methods", () => {
9798

9899
const url = new FilesRequestUrl(FilesTask.GET, "key", { timeout: 0 });
99100
const headers = new Headers();
100-
await expect(
101-
makeFilesRequest(url, headers, new Blob(), fetchStub as typeof fetch),
102-
).to.be.rejectedWith("500 AbortError");
101+
try {
102+
await makeFilesRequest(
103+
url,
104+
headers,
105+
new Blob(),
106+
fetchStub as typeof fetch,
107+
);
108+
} catch (e) {
109+
expect((e as GoogleGenerativeAIFetchError).message).to.include(
110+
"500 AbortError",
111+
);
112+
expect((e as GoogleGenerativeAIFetchError).status).to.equal(500);
113+
expect((e as GoogleGenerativeAIFetchError).statusText).to.equal(
114+
"AbortError",
115+
);
116+
}
103117
expect(fetchStub).to.be.calledOnce;
104118
});
105119
it("Network error, no response.json()", async () => {
@@ -110,9 +124,22 @@ describe("Files API - request methods", () => {
110124
} as Response);
111125
const url = new FilesRequestUrl(FilesTask.GET, "key");
112126
const headers = new Headers();
113-
await expect(
114-
makeFilesRequest(url, headers, new Blob(), fetchStub as typeof fetch),
115-
).to.be.rejectedWith(/500 Server Error/);
127+
try {
128+
await makeFilesRequest(
129+
url,
130+
headers,
131+
new Blob(),
132+
fetchStub as typeof fetch,
133+
);
134+
} catch (e) {
135+
expect((e as GoogleGenerativeAIFetchError).message).to.include(
136+
"500 Server Error",
137+
);
138+
expect((e as GoogleGenerativeAIFetchError).status).to.equal(500);
139+
expect((e as GoogleGenerativeAIFetchError).statusText).to.equal(
140+
"Server Error",
141+
);
142+
}
116143
expect(fetchStub).to.be.calledOnce;
117144
});
118145
it("Network error, includes response.json()", async () => {
@@ -124,9 +151,22 @@ describe("Files API - request methods", () => {
124151
} as Response);
125152
const url = new FilesRequestUrl(FilesTask.GET, "key");
126153
const headers = new Headers();
127-
await expect(
128-
makeFilesRequest(url, headers, new Blob(), fetchStub as typeof fetch),
129-
).to.be.rejectedWith(/500 Server Error.*extra info/);
154+
try {
155+
await makeFilesRequest(
156+
url,
157+
headers,
158+
new Blob(),
159+
fetchStub as typeof fetch,
160+
);
161+
} catch (e) {
162+
expect((e as GoogleGenerativeAIFetchError).message).to.match(
163+
/500 Server Error.+extra info/,
164+
);
165+
expect((e as GoogleGenerativeAIFetchError).status).to.equal(500);
166+
expect((e as GoogleGenerativeAIFetchError).statusText).to.equal(
167+
"Server Error",
168+
);
169+
}
130170
expect(fetchStub).to.be.calledOnce;
131171
});
132172
it("Network error, includes response.json() and details", async () => {
@@ -150,11 +190,25 @@ describe("Files API - request methods", () => {
150190
} as Response);
151191
const url = new FilesRequestUrl(FilesTask.GET, "key");
152192
const headers = new Headers();
153-
await expect(
154-
makeFilesRequest(url, headers, new Blob(), fetchStub as typeof fetch),
155-
).to.be.rejectedWith(
156-
/500 Server Error.*extra info.*generic::invalid_argument/,
157-
);
193+
try {
194+
await makeFilesRequest(
195+
url,
196+
headers,
197+
new Blob(),
198+
fetchStub as typeof fetch,
199+
);
200+
} catch (e) {
201+
expect((e as GoogleGenerativeAIFetchError).message).to.match(
202+
/500 Server Error.*extra info.*generic::invalid_argument/,
203+
);
204+
expect((e as GoogleGenerativeAIFetchError).status).to.equal(500);
205+
expect((e as GoogleGenerativeAIFetchError).statusText).to.equal(
206+
"Server Error",
207+
);
208+
expect(
209+
(e as GoogleGenerativeAIFetchError).errorDetails[0].detail,
210+
).to.include("generic::invalid_argument");
211+
}
158212
expect(fetchStub).to.be.calledOnce;
159213
});
160214
});

‎packages/main/src/files/request.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { GoogleGenerativeAIError } from "../errors";
18+
import {
19+
GoogleGenerativeAIError,
20+
GoogleGenerativeAIFetchError,
21+
} from "../errors";
1922
import {
2023
DEFAULT_API_VERSION,
2124
DEFAULT_BASE_URL,
@@ -93,26 +96,36 @@ export async function makeFilesRequest(
9396
const response = await fetchFn(url.toString(), requestInit);
9497
if (!response.ok) {
9598
let message = "";
99+
let errorDetails;
96100
try {
97101
const json = await response.json();
98102
message = json.error.message;
99103
if (json.error.details) {
100104
message += ` ${JSON.stringify(json.error.details)}`;
105+
errorDetails = json.error.details;
101106
}
102107
} catch (e) {
103108
// ignored
104109
}
105-
throw new Error(`[${response.status} ${response.statusText}] ${message}`);
110+
throw new GoogleGenerativeAIFetchError(
111+
`Error fetching from ${url.toString()}: [${response.status} ${
112+
response.statusText
113+
}] ${message}`,
114+
response.status,
115+
response.statusText,
116+
errorDetails,
117+
);
106118
} else {
107119
return response;
108120
}
109121
} catch (e) {
110-
const err = new GoogleGenerativeAIError(
111-
`Error on task type: ${url.task} fetching from ${url.toString()}: ${
112-
e.message
113-
}`,
114-
);
115-
err.stack = e.stack;
122+
let err = e;
123+
if (!(e instanceof GoogleGenerativeAIFetchError)) {
124+
err = new GoogleGenerativeAIError(
125+
`Error fetching from ${url.toString()}: ${e.message}`,
126+
);
127+
err.stack = e.stack;
128+
}
116129
throw err;
117130
}
118131
}

‎packages/main/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717

1818
export * from "../types";
1919
export * from "./gen-ai";
20+
export * from "./errors";

‎packages/main/src/requests/request.test.ts

+56-18
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
_makeRequestInternal,
2828
constructRequest,
2929
} from "./request";
30+
import { GoogleGenerativeAIFetchError } from "../errors";
3031

3132
use(sinonChai);
3233
use(chaiAsPromised);
@@ -183,8 +184,8 @@ describe("request methods", () => {
183184
statusText: "AbortError",
184185
} as Response);
185186

186-
await expect(
187-
_makeRequestInternal(
187+
try {
188+
await _makeRequestInternal(
188189
"model-name",
189190
Task.GENERATE_CONTENT,
190191
"key",
@@ -194,8 +195,16 @@ describe("request methods", () => {
194195
timeout: 0,
195196
},
196197
fetchStub as typeof fetch,
197-
),
198-
).to.be.rejectedWith("500 AbortError");
198+
);
199+
} catch (e) {
200+
expect((e as GoogleGenerativeAIFetchError).status).to.equal(500);
201+
expect((e as GoogleGenerativeAIFetchError).statusText).to.equal(
202+
"AbortError",
203+
);
204+
expect((e as GoogleGenerativeAIFetchError).message).to.include(
205+
"500 AbortError",
206+
);
207+
}
199208
expect(fetchStub).to.be.calledOnce;
200209
});
201210
it("Network error, no response.json()", async () => {
@@ -204,17 +213,25 @@ describe("request methods", () => {
204213
status: 500,
205214
statusText: "Server Error",
206215
} as Response);
207-
await expect(
208-
_makeRequestInternal(
216+
try {
217+
await _makeRequestInternal(
209218
"model-name",
210219
Task.GENERATE_CONTENT,
211220
"key",
212221
true,
213222
"",
214223
{},
215224
fetchStub as typeof fetch,
216-
),
217-
).to.be.rejectedWith(/500 Server Error/);
225+
);
226+
} catch (e) {
227+
expect((e as GoogleGenerativeAIFetchError).status).to.equal(500);
228+
expect((e as GoogleGenerativeAIFetchError).statusText).to.equal(
229+
"Server Error",
230+
);
231+
expect((e as GoogleGenerativeAIFetchError).message).to.include(
232+
"500 Server Error",
233+
);
234+
}
218235
expect(fetchStub).to.be.calledOnce;
219236
});
220237
it("Network error, includes response.json()", async () => {
@@ -224,17 +241,26 @@ describe("request methods", () => {
224241
statusText: "Server Error",
225242
json: () => Promise.resolve({ error: { message: "extra info" } }),
226243
} as Response);
227-
await expect(
228-
_makeRequestInternal(
244+
245+
try {
246+
await _makeRequestInternal(
229247
"model-name",
230248
Task.GENERATE_CONTENT,
231249
"key",
232250
true,
233251
"",
234252
{},
235253
fetchStub as typeof fetch,
236-
),
237-
).to.be.rejectedWith(/500 Server Error.*extra info/);
254+
);
255+
} catch (e) {
256+
expect((e as GoogleGenerativeAIFetchError).status).to.equal(500);
257+
expect((e as GoogleGenerativeAIFetchError).statusText).to.equal(
258+
"Server Error",
259+
);
260+
expect((e as GoogleGenerativeAIFetchError).message).to.match(
261+
/500 Server Error.*extra info/,
262+
);
263+
}
238264
expect(fetchStub).to.be.calledOnce;
239265
});
240266
it("Network error, includes response.json() and details", async () => {
@@ -256,19 +282,31 @@ describe("request methods", () => {
256282
},
257283
}),
258284
} as Response);
259-
await expect(
260-
_makeRequestInternal(
285+
286+
try {
287+
await _makeRequestInternal(
261288
"model-name",
262289
Task.GENERATE_CONTENT,
263290
"key",
264291
true,
265292
"",
266293
{},
267294
fetchStub as typeof fetch,
268-
),
269-
).to.be.rejectedWith(
270-
/500 Server Error.*extra info.*generic::invalid_argument/,
271-
);
295+
);
296+
} catch (e) {
297+
expect((e as GoogleGenerativeAIFetchError).status).to.equal(500);
298+
expect((e as GoogleGenerativeAIFetchError).statusText).to.equal(
299+
"Server Error",
300+
);
301+
expect(
302+
(e as GoogleGenerativeAIFetchError).errorDetails[0].detail,
303+
).to.equal(
304+
"[ORIGINAL ERROR] generic::invalid_argument: invalid status photos.thumbnailer.Status.Code::5: Source image 0 too short",
305+
);
306+
expect((e as GoogleGenerativeAIFetchError).message).to.match(
307+
/500 Server Error.*extra info.*generic::invalid_argument/,
308+
);
309+
}
272310
expect(fetchStub).to.be.calledOnce;
273311
});
274312
});

‎packages/main/src/requests/request.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
*/
1717

1818
import { RequestOptions } from "../../types";
19-
import { GoogleGenerativeAIError } from "../errors";
19+
import {
20+
GoogleGenerativeAIError,
21+
GoogleGenerativeAIFetchError,
22+
} from "../errors";
2023

2124
export const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com";
2225

@@ -143,22 +146,34 @@ export async function _makeRequestInternal(
143146
response = await fetchFn(request.url, request.fetchOptions);
144147
if (!response.ok) {
145148
let message = "";
149+
let errorDetails;
146150
try {
147151
const json = await response.json();
148152
message = json.error.message;
149153
if (json.error.details) {
150154
message += ` ${JSON.stringify(json.error.details)}`;
155+
errorDetails = json.error.details;
151156
}
152157
} catch (e) {
153158
// ignored
154159
}
155-
throw new Error(`[${response.status} ${response.statusText}] ${message}`);
160+
throw new GoogleGenerativeAIFetchError(
161+
`Error fetching from ${url.toString()}: [${response.status} ${
162+
response.statusText
163+
}] ${message}`,
164+
response.status,
165+
response.statusText,
166+
errorDetails,
167+
);
156168
}
157169
} catch (e) {
158-
const err = new GoogleGenerativeAIError(
159-
`Error fetching from ${url.toString()}: ${e.message}`,
160-
);
161-
err.stack = e.stack;
170+
let err = e;
171+
if (!(e instanceof GoogleGenerativeAIFetchError)) {
172+
err = new GoogleGenerativeAIError(
173+
`Error fetching from ${url.toString()}: ${e.message}`,
174+
);
175+
err.stack = e.stack;
176+
}
162177
throw err;
163178
}
164179
return response;

0 commit comments

Comments
 (0)
Please sign in to comment.