{"record":{"id":"bbb94a22e5cfdfb4","repo":"chenhg5/cc-connect","slug":"dingtalk-create-file-request-w","errorCode":null,"errorMessage":"dingtalk: create file request: %w","messagePattern":"dingtalk: create file request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"platform/dingtalk/dingtalk.go","lineNumber":1149,"sourceCode":"\t\t\"fileType\": ext,\n\t})\n\trequestBody := map[string]any{\n\t\t\"robotCode\": p.robotCode,\n\t\t\"userIds\":   []string{rc.senderStaffId},\n\t\t\"msgKey\":    \"sampleFile\",\n\t\t\"msgParam\":  string(msgParamBytes),\n\t}\n\n\tbody, err := json.Marshal(requestBody)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"dingtalk: marshal file message: %w\", err)\n\t}\n\n\treq, err := http.NewRequestWithContext(ctx, http.MethodPost,\n\t\t\"https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend\",\n\t\tbytes.NewReader(body))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"dingtalk: create file 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: send file request: %w\", err)\n\t}\n\tdefer func() { _ = resp.Body.Close() }()\n\n\trespBody, _ := io.ReadAll(resp.Body)\n\tslog.Debug(\"dingtalk: oToMessages file response\", \"status\", resp.StatusCode, \"body\", string(respBody))\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"dingtalk: send file failed: status=%d, body=%s\", resp.StatusCode, string(respBody))\n\t}\n\n\tslog.Info(\"dingtalk: file message sent\", \"media_id\", mediaID, \"name\", name, \"user\", rc.senderStaffId)","sourceCodeStart":1131,"sourceCodeEnd":1167,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/dingtalk/dingtalk.go#L1131-L1167","documentation":"This error wraps a failure from http.NewRequestWithContext while building the POST to DingTalk's oToMessages batchSend API when sending a file. NewRequestWithContext only fails on an invalid HTTP method or an unparseable URL, so in practice this almost never fires with the hardcoded constant URL. It is a defensive guard; if you see it, the request could not even be constructed.","triggerScenarios":"Calling SendFile with a reply context that yields a valid token, but http.NewRequestWithContext failing to construct the request for https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend (invalid URL/method — practically impossible with the hardcoded constants, e.g. memory exhaustion or a malformed internal URL if modified).","commonSituations":"Only seen in exotic conditions: out-of-memory, or a developer modifying the code to build the URL dynamically (e.g. from config with a bad custom endpoint) producing an unparseable URL.","solutions":["Check the wrapped error (%w) for the real cause — it names the URL parse or method problem","If the endpoint URL was made configurable, validate it with url.Parse before passing it in","Retry; transient resource exhaustion resolves itself","Report a bug if it reproduces with unmodified code"],"exampleFix":"// before (dynamic URL)\nreq, err := http.NewRequestWithContext(ctx, http.MethodPost, cfg.BaseURL+\"/v1.0/robot/oToMessages/batchSend\", bytes.NewReader(body))\n// after (validate first)\nu, err := url.Parse(cfg.BaseURL)\nif err != nil { return fmt.Errorf(\"dingtalk: invalid base url: %w\", err) }\nreq, err := http.NewRequestWithContext(ctx, http.MethodPost, u.JoinPath(\"v1.0/robot/oToMessages/batchSend\").String(), bytes.NewReader(body))","handlingStrategy":"try-catch","validationCode":"if u, err := url.Parse(\"https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend\"); err != nil { /* fail fast */ }","typeGuard":null,"tryCatchPattern":"if err := p.SendFile(ctx, rc, data, name); err != nil { if strings.Contains(err.Error(), \"create file request\") { log.Fatalf(\"request construction failed: %v\", err) } }","preventionTips":["Don't parameterize the hardcoded endpoint URL without validating it with url.Parse","Treat this error as a code bug, not a runtime condition","Wrap endpoint construction in a helper that validates once at startup"],"tags":["dingtalk","http","network"],"backgroundTag":"api-request-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"}