Handle Z.AI textual 401 errors for auth diagnostics

This commit is contained in:
William Valentin
2026-02-15 19:50:15 -08:00
parent dd15ccb927
commit d44bfc300b
3 changed files with 20 additions and 3 deletions
+16
View File
@@ -152,4 +152,20 @@ describe('OpenAIClient tool use', () => {
messages: [{ role: 'user', content: 'hello' }],
})).rejects.toThrow(/Z\.AI authentication failed \(401\)/);
});
it('rewrites Z.AI textual 401 errors when SDK status is absent', async () => {
mockCreate.mockRejectedValueOnce(new Error(
'401 You have insufficient permissions for this operation. Missing scopes: model.request.',
));
const client = new OpenAIClient({
apiKey: 'zai-key',
model: 'glm-4.7',
baseURL: 'https://api.z.ai/api/paas/v4',
});
await expect(client.chat({
messages: [{ role: 'user', content: 'hello' }],
})).rejects.toThrow(/The key lacks `model\.request` scope/);
});
});
+2 -1
View File
@@ -260,9 +260,10 @@ export class OpenAIClient implements ModelClient {
const message = error instanceof Error ? error.message : String(error);
const isZai = (this.baseURL ?? '').includes('api.z.ai');
const isUnauthorized401 = status === 401 || /\b401\b/.test(message);
const missingModelRequestScope = message.includes('Missing scopes: model.request');
if (isZai && status === 401) {
if (isZai && isUnauthorized401) {
const hint = missingModelRequestScope
? 'The key lacks `model.request` scope.'
: 'The API key is invalid, expired, or not allowed for this model/endpoint.';