{"record":{"id":"fbe6d51a52681e47","repo":"chenhg5/cc-connect","slug":"dingtalk-create-proactive-request-w","errorCode":null,"errorMessage":"dingtalk: create proactive request: %w","messagePattern":"dingtalk: create proactive request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"platform/dingtalk/dingtalk.go","lineNumber":1685,"sourceCode":"\t\tmsgParam, _ := json.Marshal(map[string]string{\"title\": cardTitleFromContent(content), \"text\": content})\n\t\trequestBody = map[string]any{\n\t\t\t\"robotCode\": p.robotCode,\n\t\t\t\"userIds\":   []string{rc.senderStaffId},\n\t\t\t\"msgKey\":    \"sampleMarkdown\",\n\t\t\t\"msgParam\":  string(msgParam),\n\t\t}\n\t} else {\n\t\treturn fmt.Errorf(\"dingtalk: proactive send requires conversationId (group) or senderStaffId (direct)\")\n\t}\n\n\tbody, err := json.Marshal(requestBody)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"dingtalk: marshal proactive message: %w\", err)\n\t}\n\n\treq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(body))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"dingtalk: create proactive request: %w\", err)\n\t}\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\treq.Header.Set(\"x-acs-dingtalk-access-token\", token)\n\n\tresp, err := p.httpClient.Do(req)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"dingtalk: proactive send request: %w\", err)\n\t}\n\tdefer func() { _ = resp.Body.Close() }()\n\n\trespBody, _ := io.ReadAll(resp.Body)\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"dingtalk: proactive send failed: status=%d, body=%s\", resp.StatusCode, string(respBody))\n\t}\n\n\tslog.Debug(\"dingtalk: proactive message sent\", \"api\", apiURL, \"status\", resp.StatusCode)\n\treturn nil\n}","sourceCodeStart":1667,"sourceCodeEnd":1703,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/dingtalk/dingtalk.go#L1667-L1703","documentation":"This error is returned when http.NewRequestWithContext fails to construct the POST request to the DingTalk proactive-send API. Given a constant API URL and an in-memory body, this failure is rare and typically signals an invalid URL or a nil context/body.","triggerScenarios":"http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(body)) returns an error — e.g. apiURL failed to parse or ctx is nil.","commonSituations":"A misconfigured base URL overriding the DingTalk API endpoint; calling the send path with context.Background() forgotten (nil context); proxy/environment manipulation corrupting URL parsing.","solutions":["Check that any configured API base URL for DingTalk is a valid absolute https URL.","Ensure a non-nil context.Context is passed into the send call.","Print the wrapped error (%w) to confirm whether it is a URL parse error.","Verify no custom transport/proxy config produces an invalid request URL."],"exampleFix":"// before\np.Send(nil, rc, msg) // nil context\n// after\nif err := p.Send(ctx, rc, msg); err != nil {\n    slog.Error(\"proactive send failed\", \"err\", err)\n}","handlingStrategy":"validation","validationCode":"if _, err := url.Parse(apiURL); err != nil {\n    return fmt.Errorf(\"bad dingtalk api url: %w\", err)\n}\nif ctx == nil {\n    return errors.New(\"nil context\")\n}","typeGuard":null,"tryCatchPattern":"err := p.Send(ctx, rc, msg)\nif err != nil && strings.Contains(err.Error(), \"create proactive request\") {\n    slog.Error(\"request construction failed\", \"err\", err)\n}","preventionTips":["Never pass a nil context; derive from context.Background() or the caller's ctx","Keep the DingTalk API base URL a constant valid https URL","Validate any custom endpoint override at config load time"],"tags":["dingtalk","http","request-construction","proactive-send"],"backgroundTag":"invalid-url","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"}