linshenkx/prompt-optimizer · error · APIError

Connection test failed: ${error.message}

Error message

Connection test failed: ${error.message}

What it means

Thrown by LLMService.testConnection when the adapter's sendMessage call fails with an error that is not already a RequestConfigError or APIError. The raw error is wrapped into an APIError with a 'Connection test failed' prefix, meaning the configured model/provider endpoint could not be reached or rejected the test message.

Source

Thrown at packages/core/src/services/llm/service.ts:228

      const testMessages: Message[] = [
        {
          role: 'user',
          content: 'Please reply ok'
        }
      ];

      this.validateMessages(testMessages);

      // Send directly through the adapter to avoid the normal "enabled" constraint.
      const adapter = this.registry.getAdapter(modelConfig.providerMeta.id);
      const runtimeConfig = this.prepareRuntimeConfig(modelConfig);
      await adapter.sendMessage(testMessages, runtimeConfig);

    } catch (error: any) {
      if (error instanceof RequestConfigError || error instanceof APIError) {
        throw error;
      }
      throw new APIError(`Connection test failed: ${error.message}`);
    }
  }

  /**
   * 获取模型列表,以下拉选项格式返回
   * @param provider 提供商标识
   * @param customConfig 自定义配置(可选)
   */
  async fetchModelList(
    provider: string,
    customConfig?: Partial<TextModelConfig> | Partial<ModelConfig>
  ): Promise<ModelOption[]> {
    try {
      // 获取基础配置
      const baseConfig = await this.modelManager.getModel(provider);
      const modelConfig = await this.buildEffectiveModelConfig(provider, baseConfig, customConfig);

      // 使用 Registry 获取模型列表

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Verify the provider baseUrl and API key in the model config are correct and reachable
  2. Test the same credentials with curl or the provider's playground
  3. Check for proxy/firewall/CORS restrictions in the runtime environment
  4. If a timeout or TypeError occurs, inspect error.message from the caught APIError for the underlying cause

Example fix

// before
const config = { ...modelConfig, baseUrl: 'https://api.example/com/v1' };
await service.testConnection('openai', config);

// after
const config = { ...modelConfig, baseUrl: 'https://api.example.com/v1' };
try {
  await service.testConnection('openai', config);
} catch (e) {
  if (e instanceof APIError) console.error('Connection test failed:', e.message);
}
Defensive patterns

Strategy: try-catch

Validate before calling

const cfgValid = Boolean(modelConfig?.baseUrl && modelConfig?.apiKey);

Type guard

function isAPIError(e: unknown): e is APIError { return e instanceof APIError; }

Try / catch

try { await service.testConnection(p, cfg); } catch (e) { if (e instanceof APIError) showUserMessage(e.message); else throw e; }

Prevention

When it happens

Trigger: Calling llmService.testConnection(provider, modelConfig) when the provider endpoint is unreachable, the API key is rejected at the network layer, or the adapter throws a non-APIError (e.g. TypeError, fetch failure, timeout).

Common situations: Wrong baseUrl in model config, expired API key, network/proxy issues, CORS in browser environments, or a malformed test message built from model parameters.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/74e296b0430bfa15. Report an issue: GitHub.