fix(gemini): preserve authorization URL error messages

This commit is contained in:
chihao.ou
2026-10-09 20:07:28 +08:00
parent 3a6fd1c9db
commit 5753552b95
2 changed files with 42 additions and 2 deletions
@@ -0,0 +1,39 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useGeminiOAuth } from '../useGeminiOAuth'
const mocks = vi.hoisted(() => ({ generateAuthUrl: vi.fn(), showError: vi.fn() }))
vi.mock('@/api/admin', () => ({ adminAPI: { gemini: { generateAuthUrl: mocks.generateAuthUrl } } }))
vi.mock('@/stores/app', () => ({ useAppStore: () => ({ showError: mocks.showError }) }))
vi.mock('vue-i18n', () => ({ useI18n: () => ({ t: (key: string) => key }) }))
beforeEach(() => vi.resetAllMocks())
describe('Gemini authorization URL errors', () => {
it.each([
[{ status: 400, message: 'Selected proxy is unavailable' }, 'Selected proxy is unavailable'],
[{ response: { data: { detail: 'OAuth is not configured' } } }, 'OAuth is not configured'],
[{}, 'admin.accounts.oauth.gemini.failedToGenerateUrl'],
[null, 'admin.accounts.oauth.gemini.failedToGenerateUrl']
])('preserves useful API feedback for %j', async (error, expected) => {
mocks.generateAuthUrl.mockRejectedValue(error)
const oauth = useGeminiOAuth()
expect(await oauth.generateAuthUrl(7)).toBe(false)
expect(oauth.error.value).toBe(expected)
expect(mocks.showError).toHaveBeenCalledWith(expected)
expect(oauth.loading.value).toBe(false)
expect(oauth.authUrl.value).toBe('')
})
it('can retry successfully after an error', async () => {
mocks.generateAuthUrl.mockRejectedValueOnce({ message: 'Temporary failure' }).mockResolvedValueOnce({
auth_url: 'https://example.com/auth', session_id: 'session', state: 'state'
})
const oauth = useGeminiOAuth()
await oauth.generateAuthUrl(7)
expect(await oauth.generateAuthUrl(7, ' project ')).toBe(true)
expect(oauth.error.value).toBe('')
expect(oauth.authUrl.value).toBe('https://example.com/auth')
expect(oauth.sessionId.value).toBe('session')
expect(oauth.state.value).toBe('state')
expect(mocks.generateAuthUrl).toHaveBeenLastCalledWith({ proxy_id: 7, project_id: 'project' })
})
})
+3 -2
View File
@@ -2,6 +2,7 @@ import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { useAppStore } from '@/stores/app'
import { adminAPI } from '@/api/admin'
import { extractApiErrorMessage } from '@/utils/apiError'
import type { GeminiOAuthCapabilities } from '@/api/admin/gemini'
export interface GeminiTokenInfo {
@@ -61,8 +62,8 @@ export function useGeminiOAuth() {
sessionId.value = response.session_id
state.value = response.state
return true
} catch (err: any) {
error.value = err.response?.data?.detail || t('admin.accounts.oauth.gemini.failedToGenerateUrl')
} catch (err: unknown) {
error.value = extractApiErrorMessage(err, t('admin.accounts.oauth.gemini.failedToGenerateUrl'))
appStore.showError(error.value)
return false
} finally {