{"record":{"id":"56f2f8daf8768eab","repo":"jeessy2/ddns-go","slug":"failed-to-parse-response-data-w","errorCode":null,"errorMessage":"failed to parse response data: %w","messagePattern":"failed to parse response data: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dns/hipmdnsmgr.go","lineNumber":248,"sourceCode":"func (h *HiPMDnsMgr) getDomainID(baseURL, apiToken, domainName string) (int, error) {\n\t// Method 1: Use keyword parameter for direct query (efficient)\n\tpath := fmt.Sprintf(\"/domains?page=1&pageSize=1&keyword=%s\", domainName)\n\n\tapiResp, err := h.request(baseURL, apiToken, \"GET\", path, nil)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\n\tif apiResp.Code != 0 {\n\t\treturn 0, fmt.Errorf(\"API error: %s\", apiResp.Msg)\n\t}\n\n\tvar domains []DnsMgrDomain\n\n\t// Smart detection: support both array and object formats\n\tvar rawData interface{}\n\tif err := json.Unmarshal(apiResp.Data, &rawData); err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to parse response data: %w\", err)\n\t}\n\n\tswitch v := rawData.(type) {\n\tcase []interface{}:\n\t\tjsonData, _ := json.Marshal(v)\n\t\tif err := json.Unmarshal(jsonData, &domains); err != nil {\n\t\t\treturn 0, fmt.Errorf(\"failed to parse domain list: %w\", err)\n\t\t}\n\tcase map[string]interface{}:\n\t\tif listData, ok := v[\"list\"]; ok {\n\t\t\tjsonData, _ := json.Marshal(listData)\n\t\t\tif err := json.Unmarshal(jsonData, &domains); err != nil {\n\t\t\t\treturn 0, fmt.Errorf(\"failed to parse domain list: %w\", err)\n\t\t\t}\n\t\t} else {\n\t\t\treturn 0, fmt.Errorf(\"invalid response format: missing list field\")\n\t\t}\n\tdefault:","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/jeessy2/ddns-go/blob/5874c2e6667c69357ab95079421131104bd3fcae/dns/hipmdnsmgr.go#L230-L266","documentation":"getDomainID could not json.Unmarshal the API response's 'data' field into a generic interface{} before format detection. This means the data field is not valid JSON (e.g. null literal handled oddly, truncated body, or a non-JSON payload string).","triggerScenarios":"The DNSMgr /domains keyword query returns HTTP 200 with a code==0 envelope whose 'data' contains invalid or unexpected JSON that cannot unmarshal into interface{}.","commonSituations":"A proxy or self-hosted reverse proxy truncating/rewriting the response; DNSMgr version emitting a data payload type the parser doesn't expect; corrupted response body from a flaky network layer decoded by the shared http.Client.","solutions":["Curl the same endpoint and inspect the raw 'data' value for validity","Check for proxies/middleware altering response bodies","Upgrade or fix the DNSMgr server so /domains returns well-formed JSON data","Capture the wrapped %w message for the precise json error (offset/syntax) to pinpoint the malformation"],"exampleFix":"// before: response\n{\"code\":0,\"data\":\"<html>error page</html>\",\"msg\":\"\"}\n// after: expected\n{\"code\":0,\"data\":{\"list\":[{\"id\":1,\"name\":\"example.com\"}]},\"msg\":\"\"}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func isValidJSON(b []byte) bool {\n    var v interface{}\n    return json.Unmarshal(b, &v) == nil\n}\n// guard the data field before parsing\nif !isValidJSON(apiResp.Data) {\n    return fmt.Errorf(\"malformed data field from API\")\n}","tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"failed to parse response data\") {\n    log.Printf(\"DNSMgr returned non-JSON data payload; check proxies/server health: %v\", err)\n    return // don't retry immediately; investigate server/proxy\n}","preventionTips":["Ensure no reverse proxy rewrites or truncates API responses","Check Content-Length vs actual body when debugging (truncated JSON)","Test /api/domains with curl after any DNSMgr server upgrade"],"tags":["go","json","parsing","dns"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"5874c2e6667c69357ab95079421131104bd3fcae","analyzedAt":"2026-09-03T15:41:16.799Z","contentChangedAt":"2026-09-03T15:41:16.799Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}