Skip to content

Commit 5fb071b

Browse files
committed
Add a new VertexAI error type
1 parent ab883d0 commit 5fb071b

17 files changed

+556
-166
lines changed

.changeset/light-cheetahs-arrive.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/vertexai-preview': patch
3+
---
4+
5+
Add a new VertexAI error type

common/api-review/vertexai-preview.api.md

+40
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { AppCheckTokenResult } from '@firebase/app-check-interop-types';
88
import { FirebaseApp } from '@firebase/app';
99
import { FirebaseAuthTokenData } from '@firebase/auth-interop-types';
10+
import { FirebaseError } from '@firebase/util';
1011

1112
// @public
1213
export interface BaseParams {
@@ -102,6 +103,16 @@ export interface EnhancedGenerateContentResponse extends GenerateContentResponse
102103
text: () => string;
103104
}
104105

106+
// @public
107+
export interface ErrorDetails {
108+
// (undocumented)
109+
'@type'?: string;
110+
[key: string]: unknown;
111+
domain?: string;
112+
metadata?: Record<string, unknown>;
113+
reason?: string;
114+
}
115+
105116
// @public
106117
export interface FileData {
107118
// (undocumented)
@@ -590,6 +601,35 @@ export interface VertexAI {
590601
location: string;
591602
}
592603

604+
// @public
605+
export class VertexAIError extends FirebaseError {
606+
constructor(code: VertexAIErrorCode, message: string, status?: number | undefined, statusText?: string | undefined, errorDetails?: ErrorDetails[] | undefined);
607+
// (undocumented)
608+
readonly code: VertexAIErrorCode;
609+
// (undocumented)
610+
readonly errorDetails?: ErrorDetails[] | undefined;
611+
// (undocumented)
612+
readonly message: string;
613+
readonly stack?: string;
614+
// (undocumented)
615+
readonly status?: number | undefined;
616+
// (undocumented)
617+
readonly statusText?: string | undefined;
618+
}
619+
620+
// @public
621+
export const enum VertexAIErrorCode {
622+
ERROR = "error",
623+
FETCH_ERROR = "fetch-error",
624+
INVALID_CONTENT = "invalid-content",
625+
NO_API_KEY = "no-api-key",
626+
NO_MODEL = "no-model",
627+
NO_PROJECT_ID = "no-project-id",
628+
PARSE_FAILED = "parse-failed",
629+
REQUEST_ERROR = "request-error",
630+
RESPONSE_ERROR = "response-error"
631+
}
632+
593633
// @public
594634
export interface VertexAIOptions {
595635
// (undocumented)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Project: /docs/reference/js/_project.yaml
2+
Book: /docs/reference/_book.yaml
3+
page_type: reference
4+
5+
{% comment %}
6+
DO NOT EDIT THIS FILE!
7+
This is generated by the JS SDK team, and any local changes will be
8+
overwritten. Changes should be made in the source code at
9+
https://github.com/firebase/firebase-js-sdk
10+
{% endcomment %}
11+
12+
# ErrorDetails interface
13+
Details object that may be included in an error response.
14+
15+
<b>Signature:</b>
16+
17+
```typescript
18+
export interface ErrorDetails
19+
```
20+
21+
## Properties
22+
23+
| Property | Type | Description |
24+
| --- | --- | --- |
25+
| ["@type"](./vertexai-preview.errordetails.md#errordetails"@type") | string | |
26+
| [domain](./vertexai-preview.errordetails.md#errordetailsdomain) | string | The domain where the error occured. |
27+
| [metadata](./vertexai-preview.errordetails.md#errordetailsmetadata) | Record&lt;string, unknown&gt; | Additonal metadata about the error. |
28+
| [reason](./vertexai-preview.errordetails.md#errordetailsreason) | string | The reason for the error. |
29+
30+
## ErrorDetails."@type"
31+
32+
<b>Signature:</b>
33+
34+
```typescript
35+
'@type'?: string;
36+
```
37+
38+
## ErrorDetails.domain
39+
40+
The domain where the error occured.
41+
42+
<b>Signature:</b>
43+
44+
```typescript
45+
domain?: string;
46+
```
47+
48+
## ErrorDetails.metadata
49+
50+
Additonal metadata about the error.
51+
52+
<b>Signature:</b>
53+
54+
```typescript
55+
metadata?: Record<string, unknown>;
56+
```
57+
58+
## ErrorDetails.reason
59+
60+
The reason for the error.
61+
62+
<b>Signature:</b>
63+
64+
```typescript
65+
reason?: string;
66+
```

docs-devsite/vertexai-preview.md

+27
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The Vertex AI For Firebase Web SDK.
2727
| --- | --- |
2828
| [ChatSession](./vertexai-preview.chatsession.md#chatsession_class) | ChatSession class that enables sending chat messages and stores history of sent and received messages so far. |
2929
| [GenerativeModel](./vertexai-preview.generativemodel.md#generativemodel_class) | Class for generative model APIs. |
30+
| [VertexAIError](./vertexai-preview.vertexaierror.md#vertexaierror_class) | Error class for the Firebase VertexAI SDK. |
3031

3132
## Enumerations
3233

@@ -41,6 +42,7 @@ The Vertex AI For Firebase Web SDK.
4142
| [HarmCategory](./vertexai-preview.md#harmcategory) | Harm categories that would cause prompts or candidates to be blocked. |
4243
| [HarmProbability](./vertexai-preview.md#harmprobability) | Probability that a prompt or candidate matches a harm category. |
4344
| [HarmSeverity](./vertexai-preview.md#harmseverity) | Harm severity levels. |
45+
| [VertexAIErrorCode](./vertexai-preview.md#vertexaierrorcode) | Standardized error codes that [VertexAIError](./vertexai-preview.vertexaierror.md#vertexaierror_class) can have. |
4446

4547
## Interfaces
4648

@@ -54,6 +56,7 @@ The Vertex AI For Firebase Web SDK.
5456
| [CountTokensResponse](./vertexai-preview.counttokensresponse.md#counttokensresponse_interface) | Response from calling [GenerativeModel.countTokens()](./vertexai-preview.generativemodel.md#generativemodelcounttokens)<!-- -->. |
5557
| [Date\_2](./vertexai-preview.date_2.md#date_2_interface) | Protobuf google.type.Date |
5658
| [EnhancedGenerateContentResponse](./vertexai-preview.enhancedgeneratecontentresponse.md#enhancedgeneratecontentresponse_interface) | Response object wrapped with helper methods. |
59+
| [ErrorDetails](./vertexai-preview.errordetails.md#errordetails_interface) | Details object that may be included in an error response. |
5760
| [FileData](./vertexai-preview.filedata.md#filedata_interface) | Data pointing to a file uploaded on Google Cloud Storage. |
5861
| [FileDataPart](./vertexai-preview.filedatapart.md#filedatapart_interface) | Content part interface if the part represents [FileData](./vertexai-preview.filedata.md#filedata_interface) |
5962
| [FunctionCall](./vertexai-preview.functioncall.md#functioncall_interface) | A predicted [FunctionCall](./vertexai-preview.functioncall.md#functioncall_interface) returned from the model that contains a string representing the [FunctionDeclaration.name](./vertexai-preview.functiondeclaration.md#functiondeclarationname) and a structured JSON object containing the parameters and their values. |
@@ -367,3 +370,27 @@ export declare enum HarmSeverity
367370
| HARM\_SEVERITY\_NEGLIGIBLE | <code>&quot;HARM_SEVERITY_NEGLIGIBLE&quot;</code> | |
368371
| HARM\_SEVERITY\_UNSPECIFIED | <code>&quot;HARM_SEVERITY_UNSPECIFIED&quot;</code> | |
369372

373+
## VertexAIErrorCode
374+
375+
Standardized error codes that [VertexAIError](./vertexai-preview.vertexaierror.md#vertexaierror_class) can have.
376+
377+
<b>Signature:</b>
378+
379+
```typescript
380+
export declare const enum VertexAIErrorCode
381+
```
382+
383+
## Enumeration Members
384+
385+
| Member | Value | Description |
386+
| --- | --- | --- |
387+
| ERROR | <code>&quot;error&quot;</code> | A generic error occured. |
388+
| FETCH\_ERROR | <code>&quot;fetch-error&quot;</code> | An error occurred while performing a fetch. |
389+
| INVALID\_CONTENT | <code>&quot;invalid-content&quot;</code> | An error associated with a Content object. |
390+
| NO\_API\_KEY | <code>&quot;no-api-key&quot;</code> | An error occured due to a missing api key. |
391+
| NO\_MODEL | <code>&quot;no-model&quot;</code> | An error occurred due to a missing model. |
392+
| NO\_PROJECT\_ID | <code>&quot;no-project-id&quot;</code> | An error occured due to a missing project id. |
393+
| PARSE\_FAILED | <code>&quot;parse-failed&quot;</code> | An error occured while parsing. |
394+
| REQUEST\_ERROR | <code>&quot;request-error&quot;</code> | An error occurred in a request. |
395+
| RESPONSE\_ERROR | <code>&quot;response-error&quot;</code> | An error occured in a response. |
396+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
Project: /docs/reference/js/_project.yaml
2+
Book: /docs/reference/_book.yaml
3+
page_type: reference
4+
5+
{% comment %}
6+
DO NOT EDIT THIS FILE!
7+
This is generated by the JS SDK team, and any local changes will be
8+
overwritten. Changes should be made in the source code at
9+
https://github.com/firebase/firebase-js-sdk
10+
{% endcomment %}
11+
12+
# VertexAIError class
13+
Error class for the Firebase VertexAI SDK.
14+
15+
<b>Signature:</b>
16+
17+
```typescript
18+
export declare class VertexAIError extends FirebaseError
19+
```
20+
<b>Extends:</b> [FirebaseError](./util.firebaseerror.md#firebaseerror_class)
21+
22+
## Constructors
23+
24+
| Constructor | Modifiers | Description |
25+
| --- | --- | --- |
26+
| [(constructor)(code, message, status, statusText, errorDetails)](./vertexai-preview.vertexaierror.md#vertexaierrorconstructor) | | Creates a new VertexAIError instance. |
27+
28+
## Properties
29+
30+
| Property | Modifiers | Type | Description |
31+
| --- | --- | --- | --- |
32+
| [code](./vertexai-preview.vertexaierror.md#vertexaierrorcode) | | [VertexAIErrorCode](./vertexai-preview.md#vertexaierrorcode) | |
33+
| [errorDetails](./vertexai-preview.vertexaierror.md#vertexaierrorerrordetails) | | [ErrorDetails](./vertexai-preview.errordetails.md#errordetails_interface)<!-- -->\[\] \| undefined | |
34+
| [message](./vertexai-preview.vertexaierror.md#vertexaierrormessage) | | string | |
35+
| [stack](./vertexai-preview.vertexaierror.md#vertexaierrorstack) | | string | Stack trace of the error. |
36+
| [status](./vertexai-preview.vertexaierror.md#vertexaierrorstatus) | | number \| undefined | |
37+
| [statusText](./vertexai-preview.vertexaierror.md#vertexaierrorstatustext) | | string \| undefined | |
38+
39+
## VertexAIError.(constructor)
40+
41+
Creates a new VertexAIError instance.
42+
43+
<b>Signature:</b>
44+
45+
```typescript
46+
constructor(code: VertexAIErrorCode, message: string, status?: number | undefined, statusText?: string | undefined, errorDetails?: ErrorDetails[] | undefined);
47+
```
48+
49+
#### Parameters
50+
51+
| Parameter | Type | Description |
52+
| --- | --- | --- |
53+
| code | [VertexAIErrorCode](./vertexai-preview.md#vertexaierrorcode) | The error code from [VertexAIErrorCode](./vertexai-preview.md#vertexaierrorcode)<!-- -->. |
54+
| message | string | A human-readable message describing the error. |
55+
| status | number \| undefined | Optional HTTP status code of the error response. |
56+
| statusText | string \| undefined | Optional HTTP status text of the error response. |
57+
| errorDetails | [ErrorDetails](./vertexai-preview.errordetails.md#errordetails_interface)<!-- -->\[\] \| undefined | Optional additional details about the error. |
58+
59+
## VertexAIError.code
60+
61+
<b>Signature:</b>
62+
63+
```typescript
64+
readonly code: VertexAIErrorCode;
65+
```
66+
67+
## VertexAIError.errorDetails
68+
69+
<b>Signature:</b>
70+
71+
```typescript
72+
readonly errorDetails?: ErrorDetails[] | undefined;
73+
```
74+
75+
## VertexAIError.message
76+
77+
<b>Signature:</b>
78+
79+
```typescript
80+
readonly message: string;
81+
```
82+
83+
## VertexAIError.stack
84+
85+
Stack trace of the error.
86+
87+
<b>Signature:</b>
88+
89+
```typescript
90+
readonly stack?: string;
91+
```
92+
93+
## VertexAIError.status
94+
95+
<b>Signature:</b>
96+
97+
```typescript
98+
readonly status?: number | undefined;
99+
```
100+
101+
## VertexAIError.statusText
102+
103+
<b>Signature:</b>
104+
105+
```typescript
106+
readonly statusText?: string | undefined;
107+
```

packages/vertexai/src/api.test.ts

+25-11
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
import { ModelParams } from './types';
17+
import { ModelParams, VertexAIError, VertexAIErrorCode } from './types';
1818
import { getGenerativeModel } from './api';
1919
import { expect } from 'chai';
2020
import { VertexAI } from './public-types';
2121
import { GenerativeModel } from './models/generative-model';
22-
import { VertexError } from './errors';
2322

2423
const fakeVertexAI: VertexAI = {
2524
app: {
@@ -35,27 +34,42 @@ const fakeVertexAI: VertexAI = {
3534

3635
describe('Top level API', () => {
3736
it('getGenerativeModel throws if no model is provided', () => {
38-
expect(() => getGenerativeModel(fakeVertexAI, {} as ModelParams)).to.throw(
39-
VertexError.NO_MODEL
40-
);
37+
try {
38+
getGenerativeModel(fakeVertexAI, {} as ModelParams);
39+
} catch (e) {
40+
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_MODEL);
41+
expect((e as VertexAIError).message).equals('Missing model parameter');
42+
}
4143
});
4244
it('getGenerativeModel throws if no apiKey is provided', () => {
4345
const fakeVertexNoApiKey = {
4446
...fakeVertexAI,
4547
app: { options: { projectId: 'my-project' } }
4648
} as VertexAI;
47-
expect(() =>
48-
getGenerativeModel(fakeVertexNoApiKey, { model: 'my-model' })
49-
).to.throw(VertexError.NO_API_KEY);
49+
try {
50+
getGenerativeModel(fakeVertexNoApiKey, { model: 'my-model' });
51+
} catch (e) {
52+
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_API_KEY);
53+
expect((e as VertexAIError).message).equals(
54+
'Missing Firebase app API key'
55+
);
56+
}
5057
});
5158
it('getGenerativeModel throws if no projectId is provided', () => {
5259
const fakeVertexNoProject = {
5360
...fakeVertexAI,
5461
app: { options: { apiKey: 'my-key' } }
5562
} as VertexAI;
56-
expect(() =>
57-
getGenerativeModel(fakeVertexNoProject, { model: 'my-model' })
58-
).to.throw(VertexError.NO_PROJECT_ID);
63+
try {
64+
getGenerativeModel(fakeVertexNoProject, { model: 'my-model' });
65+
} catch (e) {
66+
expect((e as VertexAIError).code).includes(
67+
VertexAIErrorCode.NO_PROJECT_ID
68+
);
69+
expect((e as VertexAIError).message).equals(
70+
'Missing Firebase app project ID'
71+
);
72+
}
5973
});
6074
it('getGenerativeModel gets a GenerativeModel', () => {
6175
const genModel = getGenerativeModel(fakeVertexAI, { model: 'my-model' });

packages/vertexai/src/api.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ import { getModularInstance } from '@firebase/util';
2121
import { DEFAULT_LOCATION, VERTEX_TYPE } from './constants';
2222
import { VertexAIService } from './service';
2323
import { VertexAI, VertexAIOptions } from './public-types';
24-
import { ERROR_FACTORY, VertexError } from './errors';
25-
import { ModelParams, RequestOptions } from './types';
24+
import {
25+
ModelParams,
26+
RequestOptions,
27+
VertexAIError,
28+
VertexAIErrorCode
29+
} from './types';
2630
import { GenerativeModel } from './models/generative-model';
2731

2832
export { ChatSession } from './methods/chat-session';
@@ -67,7 +71,10 @@ export function getGenerativeModel(
6771
requestOptions?: RequestOptions
6872
): GenerativeModel {
6973
if (!modelParams.model) {
70-
throw ERROR_FACTORY.create(VertexError.NO_MODEL);
74+
throw new VertexAIError(
75+
VertexAIErrorCode.NO_MODEL,
76+
'Missing model parameter'
77+
);
7178
}
7279
return new GenerativeModel(vertexAI, modelParams, requestOptions);
7380
}

0 commit comments

Comments
 (0)