Skip to content

Commit

Permalink
feat: Add GoogleApi error in ClientError.cause
Browse files Browse the repository at this point in the history
Co-authored-by: tjenkinson
FUTURE_COPYBARA_INTEGRATE_REVIEW=#418 from googleapis:release-please--branches--main--components--vertexai ac43919
PiperOrigin-RevId: 668992333
  • Loading branch information
happy-qiao authored and copybara-github committed Aug 29, 2024
1 parent 5d80976 commit 230adea
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
17 changes: 15 additions & 2 deletions src/functions/post_fetch_processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import {
StreamGenerateContentResult,
} from '../types/content';
import {constants} from '../util';
import {ClientError, GoogleGenerativeAIError} from '../types/errors';
import {
ClientError,
GoogleApiError,
GoogleGenerativeAIError,
} from '../types/errors';

export async function throwErrorIfNotOK(response: Response | undefined) {
if (response === undefined) {
Expand All @@ -40,7 +44,16 @@ export async function throwErrorIfNotOK(response: Response | undefined) {
errorBody
)}`;
if (status >= 400 && status < 500) {
throw new ClientError(errorMessage);
const error = new ClientError(
errorMessage,
new GoogleApiError(
errorBody.error.message,
errorBody.error.code,
errorBody.error.status,
errorBody.error.details
)
);
throw error;
}
throw new GoogleGenerativeAIError(errorMessage);
}
Expand Down
26 changes: 19 additions & 7 deletions src/functions/test/functions_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/

import {
ClientError,
CountTokensRequest,
FinishReason,
FunctionDeclarationSchemaType,
GenerateContentRequest,
GenerateContentResponse,
GenerateContentResponseHandler,
GenerateContentResult,
GoogleApiError,
HarmBlockThreshold,
HarmCategory,
HarmProbability,
Expand Down Expand Up @@ -324,22 +326,32 @@ describe('countTokens', () => {
ok: false,
};
const body = {
code: 400,
message: 'request is invalid',
status: 'INVALID_ARGUMENT',
error: {
code: 400,
message: 'request is invalid',
status: 'INVALID_ARGUMENT',
},
};
const response = new Response(JSON.stringify(body), fetch400Obj);
spyOn(global, 'fetch').and.resolveTo(response);

await expectAsync(
countTokens(
let error: any;
try {
await countTokens(
TEST_LOCATION,
TEST_RESOURCE_PATH,
TEST_TOKEN_PROMISE,
req,
TEST_API_ENDPOINT
)
).toBeRejected();
);
} catch (e) {
error = e;
}

expect(error).toBeInstanceOf(ClientError);
expect(error.cause).toBeInstanceOf(GoogleApiError);
expect(error.cause.code).toBe(400);
expect(error.cause.status).toEqual('INVALID_ARGUMENT');
});
});

Expand Down
29 changes: 29 additions & 0 deletions src/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@ class ClientError extends Error {
}
}

/**
* Google API Error Details object that may be included in an error response.
* See https://cloud.google.com/apis/design/errors
* @public
*/
export declare interface ErrorDetails {
'@type'?: string;
reason?: string;
domain?: string;
metadata?: Record<string, unknown>;
[key: string]: unknown;
}

/**
* GoogleApiError is thrown when http 4XX status is received.
* See https://cloud.google.com/apis/design/errors
*/
class GoogleApiError extends Error {
constructor(
message: string,
public code?: number,
public status?: string,
public errorDetails?: ErrorDetails[]
) {
super(message);
}
}

/**
* GoogleGenerativeAIError is thrown when http response is not ok and status code is not 4XX
* For details please refer to https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
Expand Down Expand Up @@ -78,6 +106,7 @@ function constructErrorMessage(

export {
ClientError,
GoogleApiError,
GoogleAuthError,
GoogleGenerativeAIError,
IllegalArgumentError,
Expand Down
9 changes: 9 additions & 0 deletions system_test/end_to_end_sample_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
TextPart,
VertexAI,
GenerateContentResponseHandler,
GoogleApiError,
} from '../src';
import {FunctionDeclarationSchemaType} from '../src/types';

Expand Down Expand Up @@ -345,6 +346,10 @@ describe('generateContentStream', () => {
`sys test failure on generateContentStream when having bad request
got wrong error message: ${e.message}`
);
expect(e.cause).toBeInstanceOf(GoogleApiError);
expect(e.cause.code).toBe(400);
expect(e.cause.status).toBe('INVALID_ARGUMENT');
expect(e.cause.message).toBeInstanceOf(String);
});
});
it('in preview should throw ClientError when having invalid input', async () => {
Expand All @@ -368,6 +373,10 @@ describe('generateContentStream', () => {
`sys test failure on generateContentStream in preview when having bad request
got wrong error message: ${e.message}`
);
expect(e.cause).toBeInstanceOf(GoogleApiError);
expect(e.cause.code).toBe(400);
expect(e.cause.status).toBe('INVALID_ARGUMENT');
expect(e.cause.message).toBeInstanceOf(String);
});
});

Expand Down

0 comments on commit 230adea

Please sign in to comment.