linshenkx/prompt-optimizer · error · APIError
Connection failed: ${error.message}
Error message
Connection failed: ${error.message} What it means
Same network-failure branch as the CORS case, but detectCrossOriginError concluded it is a plain connectivity failure — the endpoint is unreachable, refused, or DNS fails, and it's not a cross-origin browser block. Message is prefixed 'Connection failed:'.
Source
Thrown at packages/core/src/services/llm/adapters/openai-adapter.ts:177
throw new APIError('API returned empty model list')
}
return models
}
throw new APIError('Unexpected API response format')
} catch (error: any) {
console.error('[OpenAIAdapter] Failed to fetch models:', error)
// 连接错误处理(包括跨域检测)
if (error.message && (error.message.includes('Failed to fetch') ||
error.message.includes('Connection error'))) {
const isCrossOriginError = this.detectCrossOriginError(error, baseURL)
if (isCrossOriginError) {
throw new APIError(`Cross-origin connection failed: ${error.message}`)
} else {
throw new APIError(`Connection failed: ${error.message}`)
}
}
// API返回的错误信息
if (error.response?.data) {
throw new APIError(`API error: ${JSON.stringify(error.response.data)}`)
}
// 其他错误,保持原始信息
throw new APIError(error.message || 'Unknown error')
}
}
// ===== 参数定义(用于buildDefaultModel) =====
/**
* 获取参数定义
* 基于 OpenAI 官方文档: https://platform.openai.com/docs/api-reference/chat/createView on GitHub (pinned to 3e677b1d9f)
Solutions
- Confirm the server is running: curl $BASE_URL/models from the same environment
- Fix scheme/port/typos in baseURL (http vs https, correct port)
- If server-side, add reachability checks/retry with backoff for transient failures
- Check DNS/firewall/VPN if the host should be reachable
Example fix
// before
const models = await adapter.getModelsAsync()
// after
if (!(await isReachable(baseURL))) throw new Error(`LLM endpoint ${baseURL} unreachable`)
const models = await adapter.getModelsAsync() Defensive patterns
Strategy: retry
Validate before calling
async function endpointReachable(baseURL: string): Promise<boolean> {
try { const u = new URL(baseURL); await fetch(u.origin, { mode: 'no-cors' }); return true } catch { return false }
} Type guard
function isConnectionFailure(e: unknown): boolean {
return e instanceof APIError && e.message.startsWith('Connection failed')
} Try / catch
try { models = await adapter.getModelsAsync() }
catch (e) {
if (isConnectionFailure(e)) return withBackoff(() => adapter.getModelsAsync(), 3)
throw e
} Prevention
- Health-check configured endpoints at app startup
- Double-check scheme, host and port in baseURL
- Supervise local gateway processes (restart on crash)
When it happens
Trigger: getModelsAsync() against a local server that isn't running (ECONNREFUSED), wrong port, DNS failure for the base URL host, TLS errors, or no network.
Common situations: Gateway process stopped, wrong port in baseURL, typo in hostname, VPN/offline machine, server restarting.
Related errors
- Cross-origin connection failed: ${error.message}
- formatExecutionErrorMessage(error)
- GENERATION_FAILED
- GENERATION_FAILED
- UNSUPPORTED_TEST_TYPE
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/a0dfccce6521a5bf.
Report an issue: GitHub.