{"record":{"id":"77fcef3b7e2cf5e8","repo":"chenhg5/cc-connect","slug":"googlechat-parse-create-response-w","errorCode":null,"errorMessage":"googlechat: parse create response: %w","messagePattern":"googlechat: parse create response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"platform/googlechat/streaming.go","lineNumber":55,"sourceCode":"\treq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"googlechat: build request: %w\", err)\n\t}\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\tresp, err := p.doRequest(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"googlechat: send preview: %w\", err)\n\t}\n\tdefer func() {\n\t\tif err := resp.Body.Close(); err != nil {\n\t\t\tslog.Warn(\"googlechat: close preview response body\", \"error\", err)\n\t\t}\n\t}()\n\tvar msg struct {\n\t\tName string `json:\"name\"`\n\t}\n\tif err := json.NewDecoder(resp.Body).Decode(&msg); err != nil {\n\t\treturn nil, fmt.Errorf(\"googlechat: parse create response: %w\", err)\n\t}\n\t// drain any bytes json.Decoder left unread so the connection is reusable\n\t_, _ = io.Copy(io.Discard, resp.Body)\n\tif msg.Name == \"\" {\n\t\treturn nil, fmt.Errorf(\"googlechat: create response missing message name\")\n\t}\n\treturn &previewHandle{name: msg.Name}, nil\n}\n\n// buildUpdateRequest builds the PATCH URL and JSON body to update a message's text.\nfunc buildUpdateRequest(msgName, content string) (string, []byte, error) {\n\tu := chatAPIBase + msgName + \"?updateMask=text\"\n\tb, err := json.Marshal(map[string]any{\"text\": content})\n\tif err != nil {\n\t\treturn \"\", nil, fmt.Errorf(\"googlechat: marshal update body: %w\", err)\n\t}\n\treturn u, b, nil\n}","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/googlechat/streaming.go#L37-L73","documentation":"This error wraps a JSON decoding failure that occurred while parsing the response body returned by the Google Chat API after posting the initial streaming-preview message (spaces.messages.create). The library throws it because the preview handle it returns is built from the message resource `name` field, so the response body must be valid JSON. If the body cannot be decoded as JSON, the platform cannot know whether the message was created and aborts SendPreviewStart.","triggerScenarios":"json.NewDecoder(resp.Body).Decode(&msg) returns an error inside SendPreviewStart after p.doRequest succeeded: the response body is not valid JSON — e.g. an HTML error page from a proxy/gateway, a truncated response, an empty body, or a non-2xx response whose body is not JSON.","commonSituations":"Corporate proxies or API gateways returning HTML 502/504 pages with a 200-ish status line; network interruption mid-response causing a truncated body; pointing chatAPIBase at a wrong endpoint (config mistake) that returns non-JSON content; expired auth returning an unexpected non-JSON error payload.","solutions":["Check that chatAPIBase / the configured Google Chat endpoint is correct (https://chat.googleapis.com/v1/) and that no proxy is rewriting responses","Log the raw response body and HTTP status before decoding to see what was actually returned","Verify the auth token is valid; re-run `cc-connect doctor` or refresh the service-account credentials","Treat as transient and retry SendPreviewStart if the cause was a truncated/network response"],"exampleFix":"// before\ndecodeErr := json.NewDecoder(resp.Body).Decode(&msg)\n// after: capture body for diagnostics on failure\nraw, _ := io.ReadAll(resp.Body)\nvar msg struct{ Name string `json:\"name\"` }\nif err := json.Unmarshal(raw, &msg); err != nil {\n    return nil, fmt.Errorf(\"googlechat: parse create response: %w (body: %.200s)\", err, raw)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"handle, err := p.SendPreviewStart(ctx, rctx, initialText)\nif err != nil {\n    if strings.Contains(err.Error(), \"parse create response\") {\n        slog.Warn(\"googlechat preview: non-JSON API response; check proxy/endpoint/auth\", \"error\", err)\n    }\n    return fmt.Errorf(\"start streaming preview: %w\", err)\n}","preventionTips":["Use the default chatAPIBase endpoint; avoid proxies that inject HTML error pages","Keep service-account credentials valid and rotated so auth failures return expected JSON","Monitor network stability; retry transient truncations"],"tags":["googlechat","json","http-response","streaming-preview"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}