mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
Improve Auth error messaging (#1358)
This commit is contained in:
committed by
GitHub
parent
104f23da90
commit
0abd2a644e
@@ -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
|
||||
);
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user