Skip to content

Commit 40841b8

Browse files
committed
Add new endpoint and error message.
1 parent a377fb2 commit 40841b8

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

packages/vertexai/src/constants.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export const VERTEX_TYPE = 'vertexAI';
2121

2222
export const DEFAULT_LOCATION = 'us-central1';
2323

24-
export const DEFAULT_BASE_URL = 'https://firebaseml.googleapis.com';
24+
export const DEFAULT_BASE_URL = 'https://firebasevertexai.googleapis.com';
2525

26-
export const DEFAULT_API_VERSION = 'v2beta';
26+
export const DEFAULT_API_VERSION = 'v1beta';
2727

2828
export const PACKAGE_VERSION = version;
2929

packages/vertexai/src/methods/generate-content.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,18 @@ describe('generateContent()', () => {
220220
).to.be.rejectedWith(/400.*invalid argument/);
221221
expect(mockFetch).to.be.called;
222222
});
223+
it('api not enabled (403)', async () => {
224+
const mockResponse = getMockResponse(
225+
'unary-failure-firebasevertexai-api-not-enabled.json'
226+
);
227+
const mockFetch = stub(globalThis, 'fetch').resolves({
228+
ok: false,
229+
status: 403,
230+
json: mockResponse.json
231+
} as Response);
232+
await expect(
233+
generateContent(fakeApiSettings, 'model', fakeRequestParams)
234+
).to.be.rejectedWith(/firebasevertexai\.googleapis.*my-project/);
235+
expect(mockFetch).to.be.called;
236+
});
223237
});

packages/vertexai/src/requests/request.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { ApiSettings } from '../types/internal';
2424
import { DEFAULT_API_VERSION } from '../constants';
2525
import { VertexAIErrorCode } from '../types';
2626
import { VertexAIError } from '../errors';
27+
import { getMockResponse } from '../../test-utils/mock-response';
2728

2829
use(sinonChai);
2930
use(chaiAsPromised);
@@ -357,4 +358,28 @@ describe('request methods', () => {
357358
expect(fetchStub).to.be.calledOnce;
358359
});
359360
});
361+
it('Network error, API not enabled', async () => {
362+
const mockResponse = getMockResponse(
363+
'unary-failure-firebasevertexai-api-not-enabled.json'
364+
);
365+
const fetchStub = stub(globalThis, 'fetch').resolves(
366+
mockResponse as Response
367+
);
368+
try {
369+
await makeRequest(
370+
'models/model-name',
371+
Task.GENERATE_CONTENT,
372+
fakeApiSettings,
373+
false,
374+
''
375+
);
376+
} catch (e) {
377+
expect((e as VertexAIError).code).to.equal(
378+
VertexAIErrorCode.API_NOT_ENABLED
379+
);
380+
expect((e as VertexAIError).message).to.include('my-project');
381+
expect((e as VertexAIError).message).to.include('googleapis.com');
382+
}
383+
expect(fetchStub).to.be.calledOnce;
384+
});
360385
});

packages/vertexai/src/requests/request.ts

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

18-
import { RequestOptions, VertexAIErrorCode } from '../types';
18+
import { ErrorDetails, RequestOptions, VertexAIErrorCode } from '../types';
1919
import { VertexAIError } from '../errors';
2020
import { ApiSettings } from '../types/internal';
2121
import {
@@ -151,6 +151,34 @@ export async function makeRequest(
151151
} catch (e) {
152152
// ignored
153153
}
154+
if (
155+
response.status === 403 &&
156+
errorDetails.some(
157+
(detail: ErrorDetails) => detail.reason === 'SERVICE_DISABLED'
158+
) &&
159+
errorDetails.some((detail: ErrorDetails) =>
160+
(
161+
detail.links as Array<Record<string, string>>
162+
)?.[0]?.description.includes(
163+
'Google developers console API activation'
164+
)
165+
)
166+
) {
167+
throw new VertexAIError(
168+
VertexAIErrorCode.API_NOT_ENABLED,
169+
`The Vertex AI for Firebase SDK requires the Firebase Vertex AI API ` +
170+
`firebasevertexai.googleapis.com to be enabled for your ` +
171+
`project. Get started in the Firebase Console` +
172+
` (https://console.firebase.google.com/project/${url.apiSettings.project}/genai/vertex)` +
173+
` or verify that the API is enabled in the Google Cloud` +
174+
` Console (https://console.developers.google.com/apis/api/firebasevertexai.googleapis.com/overview?project=${url.apiSettings.project}).`,
175+
{
176+
status: response.status,
177+
statusText: response.statusText,
178+
errorDetails
179+
}
180+
);
181+
}
154182
throw new VertexAIError(
155183
VertexAIErrorCode.FETCH_ERROR,
156184
`Error fetching from ${url}: [${response.status} ${response.statusText}] ${message}`,
@@ -165,6 +193,7 @@ export async function makeRequest(
165193
let err = e as Error;
166194
if (
167195
(e as VertexAIError).code !== VertexAIErrorCode.FETCH_ERROR &&
196+
(e as VertexAIError).code !== VertexAIErrorCode.API_NOT_ENABLED &&
168197
e instanceof Error
169198
) {
170199
err = new VertexAIError(

packages/vertexai/src/types/error.ts

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export const enum VertexAIErrorCode {
7878
/** An error associated with a Content object. */
7979
INVALID_CONTENT = 'invalid-content',
8080

81+
/** An error due to the Firebase API not being enabled in the Console. */
82+
API_NOT_ENABLED = 'api-not-enabled',
83+
8184
/** An error occurred due to a missing Firebase API key. */
8285
NO_API_KEY = 'no-api-key',
8386

0 commit comments

Comments
 (0)