fix(coding-agent): prevent crash on OAuth authentication failure (#849)
Detect OAuth authentication failures (expired credentials, offline) and provide helpful error message instead of crashing with generic 'No API key found' error. Co-authored-by: Mario Zechner <badlogicgames@gmail.com>
This commit is contained in:
@@ -634,6 +634,14 @@ export class AgentSession {
|
|||||||
// Validate API key
|
// Validate API key
|
||||||
const apiKey = await this._modelRegistry.getApiKey(this.model);
|
const apiKey = await this._modelRegistry.getApiKey(this.model);
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
|
const isOAuth = this._modelRegistry.isUsingOAuth(this.model);
|
||||||
|
if (isOAuth) {
|
||||||
|
throw new Error(
|
||||||
|
`Authentication failed for "${this.model.provider}". ` +
|
||||||
|
`Credentials may have expired or network is unavailable. ` +
|
||||||
|
`Run '/login ${this.model.provider}' to re-authenticate.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`No API key found for ${this.model.provider}.\n\n` +
|
`No API key found for ${this.model.provider}.\n\n` +
|
||||||
`Use /login, set an API key environment variable, or create ${getAuthPath()}`,
|
`Use /login, set an API key environment variable, or create ${getAuthPath()}`,
|
||||||
|
|||||||
@@ -641,7 +641,18 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
|
|||||||
}
|
}
|
||||||
const key = await modelRegistry.getApiKeyForProvider(resolvedProvider);
|
const key = await modelRegistry.getApiKeyForProvider(resolvedProvider);
|
||||||
if (!key) {
|
if (!key) {
|
||||||
throw new Error(`No API key found for provider "${resolvedProvider}"`);
|
const isOAuth = modelRegistry.isUsingOAuth(currentModel);
|
||||||
|
if (isOAuth) {
|
||||||
|
throw new Error(
|
||||||
|
`Authentication failed for "${currentModel.provider}". ` +
|
||||||
|
`Credentials may have expired or network is unavailable. ` +
|
||||||
|
`Run '/login ${currentModel.provider}' to re-authenticate.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw new Error(
|
||||||
|
`No API key found for "${currentModel.provider}". ` +
|
||||||
|
`Set an API key environment variable or run '/login ${currentModel.provider}'.`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return key;
|
return key;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user