jeessy2/ddns-go · error
API请求失败,状态码: %d, 响应: %s
Error message
API请求失败,状态码: %d, 响应: %s
What it means
dns/nowcn.go returns this when the Nowcn API answers with an HTTP status other than 200. The message embeds the status code and the raw response body, so it is a generic non-2xx API rejection inside the shared request() helper (create, modify, getRecordList).
Source
Thrown at dns/nowcn.go:273
req.Header.Set("Accept", "application/json")
// 发送请求
client := t.httpClient
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("请求失败: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取响应失败: %v", err)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API请求失败,状态码: %d, 响应: %s", resp.StatusCode, string(body))
}
return body, nil
}
View on GitHub (pinned to 5874c2e666)
Solutions
- Read the embedded response body in the message — it usually names the exact API-side problem
- Verify Nowcn API credentials (key/secret) in your configuration
- Confirm the domain and record parameters match what the Nowcn dashboard shows
- If status is 5xx, wait and retry; contact Nowcn support if persistent
Example fix
// before
return nil, fmt.Errorf("API请求失败,状态码: %d, 响应: %s", resp.StatusCode, string(body))
// after (caller: branch on auth vs transient)
var nerr *fmt.Error // wrap or parse from message
if strings.Contains(err.Error(), "状态码: 401") || strings.Contains(err.Error(), "状态码: 403") {
// refresh credentials in config and re-run
} else if strings.Contains(err.Error(), "状态码: 5") {
// retry with backoff
} Defensive patterns
Strategy: type-guard
Validate before calling
// validate credentials present before calling
if apiKey == "" || apiPassword == "" {
return errors.New("nowcn: missing API credentials")
} Type guard
// classify the wrapped status-code error
func isNon200(err error) bool {
return err != nil && strings.Contains(err.Error(), "API请求失败,状态码:")
}
func statusFrom(err error) string {
i := strings.Index(err.Error(), "状态码: ")
if i < 0 { return "" }
return err.Error()[i+len("状态码: "):][:3]
} Try / catch
if err != nil {
if isNon200(err) {
switch code := statusFrom(err); {
case code == "401" || code == "403":
log.Fatal("nowcn: bad credentials — fix config")
case strings.HasPrefix(code, "5"):
// transient: schedule retry
}
}
} Prevention
- Rotate and verify Nowcn credentials on a schedule
- Log the full error (it embeds the response body) for diagnosis
- Alert on 5xx spikes rather than retrying indefinitely
When it happens
Trigger: Any Nowcn API call that receives 401/403 (bad API credentials), 4xx (malformed parameters), or 5xx (server-side error) instead of HTTP 200.
Common situations: Expired or wrong API key/password in config; domain name not owned by the account; parameter formatting changes after provider API updates; Nowcn server outages returning 5xx.
Related errors
AI-assisted analysis of jeessy2/ddns-go@5874c2e666 (2026-09-03).
Data as JSON: /api/errors/5d7a09d07500c9d5.
Report an issue: GitHub.