flipped-aurora/gin-vue-admin · error

调用上游大模型服务失败: %w

Error message

调用上游大模型服务失败: %w

What it means

LLMAuto wraps any transport-layer failure from the HTTP call to the upstream LLM service in this generic message, preserving the underlying error via %w. It means the POST issued by request.HttpRequestWithContextAndTimeout (server/service/system/auto_code_llm.go:24-33) never completed — connection, DNS, TLS, timeout, or context cancellation. The library throws it because the request phase failed before any HTTP response could be inspected.

Source

Thrown at server/service/system/auto_code_llm.go:33

	"github.com/goccy/go-json"
)

func (s *AutoCodeService) LLMAuto(ctx context.Context, llm common.JSONMap) (interface{}, error) {
	path, err := buildLLMAutoPath(llm)
	if err != nil {
		return nil, err
	}

	res, err := request.HttpRequestWithContextAndTimeout(
		ctx,
		path,
		http.MethodPost,
		nil,
		nil,
		llm,
	)
	if err != nil {
		return nil, fmt.Errorf("调用上游大模型服务失败: %w", err)
	}
	defer res.Body.Close()

	body, err := io.ReadAll(res.Body)
	if err != nil {
		return nil, fmt.Errorf("读取大模型响应失败: %w", err)
	}

	bodyPreview := previewResponseBody(body)
	contentType := res.Header.Get("Content-Type")
	if res.StatusCode < 200 || res.StatusCode >= 300 {
		return nil, fmt.Errorf("上游大模型服务返回非 2xx: status=%d content-type=%s body=%s", res.StatusCode, contentType, bodyPreview)
	}

	var resStruct commonResp.Response
	if err = json.Unmarshal(body, &resStruct); err != nil {
		return nil, fmt.Errorf("解析大模型响应失败: status=%d content-type=%s body=%s err=%w", res.StatusCode, contentType, bodyPreview, err)
	}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Verify the LLM base URL/path in the llm config map (and buildLLMAutoPath) points to a reachable, scheme-correct endpoint
  2. Test connectivity from the server host: curl the upstream URL to rule out DNS/firewall/proxy issues
  3. Increase the request timeout or check that ctx is not being cancelled early by the caller
  4. Unwrap the wrapped error (%w) with errors.Unwrap/log the original to see the exact transport cause

Example fix

// before: no reachability check, hard failure
res, err := request.HttpRequestWithContextAndTimeout(ctx, path, http.MethodPost, nil, nil, llm)
if err != nil { return nil, fmt.Errorf("调用上游大模型服务失败: %w", err) }
// after: pre-validate config and add bounded retry
if base, ok := llm["base_url"].(string); !ok || !strings.HasPrefix(base, "http") {
    return nil, fmt.Errorf("LLM base_url 配置无效: %v", llm["base_url"])
}
res, err := request.HttpRequestWithContextAndTimeout(ctx, path, http.MethodPost, nil, nil, llm)
if err != nil {
    if errors.Is(err, context.DeadlineExceeded) { /* retry once or surface timeout hint */ }
    return nil, fmt.Errorf("调用上游大模型服务失败: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

u, err := url.Parse(baseURL)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
    return fmt.Errorf("无效的 LLM 地址: %q", baseURL)
}
if conn, err := net.DialTimeout("tcp", net.JoinHostPort(u.Hostname(), portOrDefault(u, "443")), 3*time.Second); err == nil {
    conn.Close()
}

Try / catch

data, err := svc.LLMAuto(ctx, llmCfg)
if err != nil {
    var netErr net.Error
    if errors.As(err, &netErr) && netErr.Timeout() {
        // retry with backoff or return timeout-specific message
    }
    return fmt.Errorf("LLM 调用不可达: %w", err)
}

Prevention

When it happens

Trigger: Calling AutoCodeService.LLMAuto when the upstream endpoint is unreachable, the URL/path built by buildLLMAutoPath is wrong, DNS fails, TLS handshake fails, the context is cancelled, or the request timeout expires.

Common situations: LLM base URL misconfigured (wrong host/port, missing scheme), upstream provider down or behind a firewall/proxy, API host blocked in an offline/air-gapped environment, invalid self-signed certs, or the caller's ctx cancelled mid-request.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/2f6e63931f570135. Report an issue: GitHub.