Skip to content
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

Add more properties to errors and export errors. #112

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quick-actors-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@google/generative-ai": minor
---

Export error classes and add more properties to fetch errors.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md) &gt; [(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 | |

21 changes: 21 additions & 0 deletions docs/reference/main/generative-ai.googlegenerativeaierror.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIError](./generative-ai.googlegenerativeaierror.md)

## GoogleGenerativeAIError class

Basic error type for this SDK.

**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 <code>GoogleGenerativeAIError</code> class |

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) &gt; [(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)_ |

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) &gt; [errorDetails](./generative-ai.googlegenerativeaifetcherror.errordetails.md)

## GoogleGenerativeAIFetchError.errorDetails property

**Signature:**

```typescript
errorDetails?: ErrorDetails[];
```
29 changes: 29 additions & 0 deletions docs/reference/main/generative-ai.googlegenerativeaifetcherror.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md)

## 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
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 <code>GoogleGenerativeAIFetchError</code> 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)_ |

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) &gt; [status](./generative-ai.googlegenerativeaifetcherror.status.md)

## GoogleGenerativeAIFetchError.status property

**Signature:**

```typescript
status?: number;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIFetchError](./generative-ai.googlegenerativeaifetcherror.md) &gt; [statusText](./generative-ai.googlegenerativeaifetcherror.statustext.md)

## GoogleGenerativeAIFetchError.statusText property

**Signature:**

```typescript
statusText?: string;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) &gt; [(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)_ |

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md)

## 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
export declare class GoogleGenerativeAIResponseError<T> 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 <code>GoogleGenerativeAIResponseError</code> class |

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [response?](./generative-ai.googlegenerativeairesponseerror.response.md) | | T | _(Optional)_ |

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [GoogleGenerativeAIResponseError](./generative-ai.googlegenerativeairesponseerror.md) &gt; [response](./generative-ai.googlegenerativeairesponseerror.response.md)

## GoogleGenerativeAIResponseError.response property

**Signature:**

```typescript
response?: T;
```
3 changes: 3 additions & 0 deletions docs/reference/main/generative-ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | 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

Expand Down
36 changes: 36 additions & 0 deletions packages/main/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -31,3 +40,30 @@ export class GoogleGenerativeAIResponseError<
super(message);
}
}

/**
* Error class covering http errors when calling the server. Includes http
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to capitalize http in our Firebase JS SDK's docs:

See here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

* status, statusText, and optional details, if provided in the server response.
*/
export class GoogleGenerativeAIFetchError extends GoogleGenerativeAIError {
constructor(
message: string,
public status?: number,
public statusText?: string,
public errorDetails?: ErrorDetails[],
) {
super(message);
}
}

/**
* Details object that may be included in an error response.
* @public
*/
interface ErrorDetails {
"@type"?: string;
reason?: string;
domain?: string;
metadata?: Record<string, unknown>;
[key: string]: unknown;
}
82 changes: 68 additions & 14 deletions packages/main/src/files/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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;
});
});
Expand Down
Loading
Loading