Improve Auth error messaging (#1358)

This commit is contained in:
Tommaso Sciortino
2025-06-23 18:37:41 -07:00
committed by GitHub
parent 104f23da90
commit 0abd2a644e
9 changed files with 57 additions and 31 deletions

View File

@@ -1,13 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { GaxiosError } from 'gaxios';
export function isAuthError(error: unknown): boolean {
return (
error instanceof GaxiosError && error.response?.data?.error?.code === 401
);
}

View File

@@ -292,8 +292,7 @@ export class GeminiClient {
throw error;
}
try {
const parsedJson = JSON.parse(text);
return parsedJson;
return JSON.parse(text);
} catch (parseError) {
await reportError(
parseError,

View File

@@ -20,7 +20,7 @@ import { getResponseText } from '../utils/generateContentResponseUtilities.js';
import { reportError } from '../utils/errorReporting.js';
import { getErrorMessage } from '../utils/errors.js';
import { GeminiChat } from './geminiChat.js';
import { isAuthError } from '../code_assist/errors.js';
import { UnauthorizedError, toFriendlyError } from '../utils/errors.js';
// Define a structure for tools passed to the server
export interface ServerTool {
@@ -224,8 +224,9 @@ export class Turn {
value: { ...this.lastUsageMetadata, apiTimeMs: durationMs },
};
}
} catch (error) {
if (isAuthError(error)) {
} catch (e) {
const error = toFriendlyError(e);
if (error instanceof UnauthorizedError) {
throw error;
}
if (signal.aborted) {

View File

@@ -21,7 +21,6 @@ export * from './core/nonInteractiveToolExecutor.js';
export * from './code_assist/codeAssist.js';
export * from './code_assist/oauth2.js';
export * from './code_assist/errors.js';
// Export utilities
export * from './utils/paths.js';

View File

@@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { GaxiosError } from 'gaxios';
export function isNodeError(error: unknown): error is NodeJS.ErrnoException {
return error instanceof Error && 'code' in error;
}
@@ -11,12 +13,50 @@ export function isNodeError(error: unknown): error is NodeJS.ErrnoException {
export function getErrorMessage(error: unknown): string {
if (error instanceof Error) {
return error.message;
} else {
try {
const errorMessage = String(error);
return errorMessage;
} catch {
return 'Failed to get error details';
}
}
try {
return String(error);
} catch {
return 'Failed to get error details';
}
}
export class ForbiddenError extends Error {}
export class UnauthorizedError extends Error {}
export class BadRequestError extends Error {}
interface ResponseData {
error?: {
code?: number;
message?: string;
};
}
export function toFriendlyError(error: unknown): unknown {
if (error instanceof GaxiosError) {
const data = parseResponseData(error);
if (data.error && data.error.message && data.error.code) {
switch (data.error.code) {
case 400:
return new BadRequestError(data.error.message);
case 401:
return new UnauthorizedError(data.error.message);
case 403:
// It's import to pass the message here since it might
// explain the cause like "the cloud project you're
// using doesn't have code assist enabled".
return new ForbiddenError(data.error.message);
default:
}
}
}
return error;
}
function parseResponseData(error: GaxiosError): ResponseData {
// Inexplicably, Gaxios sometimes doesn't JSONify the response data.
if (typeof error.response?.data === 'string') {
return JSON.parse(error.response?.data) as ResponseData;
}
return typeof error.response?.data as ResponseData;
}