fix: Fetch error shown as string (not [object Object]); extract API error from response body; guard fetch availability

This commit is contained in:
Hermes Agent committed 2026-09-19 17:06:36 +02:00
1 parent e6b5a87f24
commit d62c453ff5
3 files changed
+24 -6

No files matched your search

+15 -3
View File
@@ -504,14 +504,25 @@ export default function () {
// UI asks to list models for a provider. Network must run here (main thread) —
// the plugin UI iframe is CSP-sandboxed and cannot fetch external URLs.
on('FETCH_MODELS', async function (payload: { baseUrl: string, apiKey: string }) {
on('FETCH_MODELS', async function (payload: any) {
try {
if (typeof fetch !== 'function') {
emit('MODELS_RESULT', { ok: false, error: 'fetch is not available in the plugin main thread', models: [] })
return
}
const base = payload.baseUrl.replace(/\/+$/, '')
const res = await fetch(base + '/models', {
headers: { Authorization: 'Bearer ' + payload.apiKey }
})
if (!res.ok) {
emit('MODELS_RESULT', { ok: false, error: 'HTTP ' + res.status, models: [] })
let msg = 'HTTP ' + res.status
try {
const body = JSON.parse(await res.text())
if (body != null && body.error != null) {
msg = typeof body.error === 'string' ? body.error : (body.error.message || JSON.stringify(body.error))
}
} catch (e) { /* keep HTTP status message */ }
emit('MODELS_RESULT', { ok: false, error: msg, models: [] })
return
}
const json: any = await res.json()
@@ -523,7 +534,8 @@ export default function () {
}
emit('MODELS_RESULT', { ok: true, models })
} catch (e) {
emit('MODELS_RESULT', { ok: false, error: String(e), models: [] })
const msg = e instanceof Error ? e.message : (e != null ? String(e) : 'Unknown error')
emit('MODELS_RESULT', { ok: false, error: msg, models: [] })
}
})