{"record":{"id":"81cd36e8e4459158","repo":"chenhg5/cc-connect","slug":"dingtalk-create-image-request-w","errorCode":null,"errorMessage":"dingtalk: create image request: %w","messagePattern":"dingtalk: create image request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"platform/dingtalk/dingtalk.go","lineNumber":1054,"sourceCode":"\n\tmsgParamBytes, _ := json.Marshal(map[string]string{\"photoURL\": mediaID})\n\trequestBody := map[string]any{\n\t\t\"robotCode\": p.robotCode,\n\t\t\"userIds\":   []string{rc.senderStaffId},\n\t\t\"msgKey\":    \"sampleImageMsg\",\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 image 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 image 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 image request: %w\", err)\n\t}\n\tdefer func() { _ = resp.Body.Close() }()\n\n\trespBody, _ := io.ReadAll(resp.Body)\n\tslog.Debug(\"dingtalk: oToMessages image response\", \"status\", resp.StatusCode, \"body\", string(respBody))\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"dingtalk: send image failed: status=%d, body=%s\", resp.StatusCode, string(respBody))\n\t}\n\n\tslog.Info(\"dingtalk: image message sent\", \"media_id\", mediaID, \"user\", rc.senderStaffId)","sourceCodeStart":1036,"sourceCodeEnd":1072,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/dingtalk/dingtalk.go#L1036-L1072","documentation":"Returned by SendImage when http.NewRequestWithContext fails to construct the POST to DingTalk's robot/oToMessages/batchSend endpoint. This only happens if the HTTP method or URL string is invalid, or ctx is already cancelled, so it indicates a programming bug or an expired context rather than a runtime network issue.","triggerScenarios":"Calling SendImage while the passed context is already cancelled/deadline-exceeded, or (theoretically) if the hard-coded endpoint URL is malformed.","commonSituations":"Caller passes a context whose deadline lapsed before the send; shutdown race where engine cancels ctx then still calls SendImage.","solutions":["Check that the context passed to SendImage is alive at call time; do not reuse a cancelled context","Wrap the call and inspect errors.Is(err, context.Canceled) / context.DeadlineExceeded to distinguish cancellation from a bug","Verify the platform code version — the URL is hard-coded, so a malformed URL would mean a corrupted build","If cancellation is expected during shutdown, tolerate this error and retry after re-init"],"exampleFix":"// before\nif err := p.SendImage(ctx, rc, img); err != nil { log.Fatal(err) }\n// after\nif err := p.SendImage(ctx, rc, img); err != nil {\n    if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {\n        return // shutdown path\n    }\n    log.Fatal(err)\n}","handlingStrategy":"try-catch","validationCode":"if ctx.Err() != nil {\n    return fmt.Errorf(\"context already done, skip SendImage: %w\", ctx.Err())\n}","typeGuard":null,"tryCatchPattern":"if err := p.SendImage(ctx, rc, img); err != nil {\n    if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {\n        return nil // expected during shutdown\n    }\n    return fmt.Errorf(\"dingtalk image send failed: %w\", err)\n}","preventionTips":["Always pass a live, non-cancelled context to platform send calls","Set context deadlines generously enough for network round trips","Log wrapped errors with %w so root causes stay inspectable"],"tags":["network","http","dingtalk","context-canceled"],"backgroundTag":"http-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"}