Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 8 additions & 11 deletions src/functions/post_fetch_processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,16 @@ export async function throwErrorIfNotOK(response: Response | undefined) {
const errorMessage = `got status: ${status} ${statusText}. ${JSON.stringify(
errorBody
)}`;
const apiError = new GoogleApiError(
errorBody.error.message,
errorBody.error.code,
errorBody.error.status,
errorBody.error.details
);
if (status >= 400 && status < 500) {
const error = new ClientError(
errorMessage,
new GoogleApiError(
errorBody.error.message,
errorBody.error.code,
errorBody.error.status,
errorBody.error.details
)
);
throw error;
throw new ClientError(errorMessage, apiError);
}
throw new GoogleGenerativeAIError(errorMessage);
throw new GoogleGenerativeAIError(errorMessage, apiError);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ class GoogleAuthError extends Error {
*/
class ClientError extends Error {
public readonly stackTrace?: Error;
public readonly statusCode?: number;

constructor(message: string, stackTrace?: Error) {
super(message, {cause: stackTrace});
this.message = constructErrorMessage('ClientError', message);
this.name = 'ClientError';
this.stackTrace = stackTrace;

if (stackTrace instanceof GoogleApiError) {
this.statusCode = stackTrace.code;
}
}
}

Expand Down Expand Up @@ -76,11 +82,17 @@ class GoogleApiError extends Error {
*/
class GoogleGenerativeAIError extends Error {
public readonly stackTrace?: Error;
public readonly statusCode?: number;

constructor(message: string, stackTrace?: Error) {
super(message, {cause: stackTrace});
this.message = constructErrorMessage('GoogleGenerativeAIError', message);
this.name = 'GoogleGenerativeAIError';
this.stackTrace = stackTrace;

if (stackTrace instanceof GoogleApiError) {
this.statusCode = stackTrace.code;
}
}
}

Expand Down