chenhg5/cc-connect · error

wecom-ws: send image: %w

Error message

wecom-ws: send image: %w

What it means

This is the wrapped error returned when the chunked media upload step (uploadWSMedia, which performs aibot_upload_media_init/chunk/finish frames over the WeCom AI Bot WebSocket) fails while sending an image. The original cause is preserved via %w; typical causes include socket not connected, request timeouts waiting for a response frame, oversized media (>100 chunks / ~50MB), or WeCom API error responses.

Source

Thrown at platform/wecom/websocket_outbound_media.go:36

	wecomWSUploadMaxChunks = 100
)

// SendImage uploads and sends an image through the WeCom AI Bot WebSocket API.
func (p *WSPlatform) SendImage(ctx context.Context, rctx any, img core.ImageAttachment) error {
	rc, ok := rctx.(wsReplyContext)
	if !ok {
		return fmt.Errorf("wecom-ws: SendImage: invalid reply context type %T", rctx)
	}
	if rc.chatID == "" {
		return fmt.Errorf("wecom-ws: chatID is empty, cannot send image")
	}
	if len(img.Data) == 0 {
		return fmt.Errorf("wecom-ws: image data is empty")
	}

	mediaID, err := p.uploadWSMedia(ctx, "image", wsImageFileName(img), img.Data)
	if err != nil {
		return fmt.Errorf("wecom-ws: send image: %w", err)
	}
	if err := p.sendWSMediaMessage(ctx, rc.chatID, "image", mediaID); err != nil {
		return fmt.Errorf("wecom-ws: send image: %w", err)
	}
	return nil
}

func (p *WSPlatform) uploadWSMedia(ctx context.Context, mediaType, filename string, data []byte) (string, error) {
	totalChunks := (len(data) + wecomWSUploadChunkSize - 1) / wecomWSUploadChunkSize
	if totalChunks == 0 {
		return "", fmt.Errorf("empty media data")
	}
	if totalChunks > wecomWSUploadMaxChunks {
		return "", fmt.Errorf("media too large: %d chunks exceeds maximum %d", totalChunks, wecomWSUploadMaxChunks)
	}

	sum := md5.Sum(data)
	initReqID := p.generateReqID("aibot_upload_media_init")

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Unwrap the error (errors.Unwrap / %v of the chain) to see the root cause and address it specifically.
  2. Verify the WSPlatform WebSocket connection is established and healthy before sending; re-send after reconnect.
  3. Reduce image size below the 100-chunk (~50MB) upload limit or compress before sending.
  4. Retry transient timeouts; if a specific req_id never gets a response, check WeCom AI Bot API status and response handling in uploadWSMedia.

Example fix

// before
if err := platform.SendImage(ctx, rc, bigImg); err != nil { return err } // opaque
// after
if err := platform.SendImage(ctx, rc, img); err != nil {
    return fmt.Errorf("send failed: %w", err) // log the unwrapped cause
}
Defensive patterns

Strategy: retry

Validate before calling

const maxWSMediaChunks = 100
if len(img.Data) > maxWSMediaChunks*512*1024 {
    return errors.New("image exceeds WeCom WS upload limit (~50MB)")
}
if !platform.IsConnected() {
    return errors.New("websocket not connected")
}

Try / catch

err := platform.SendImage(ctx, rctx, img)
for i := 0; i < 3 && err != nil; i++ {
    if errors.Is(err, context.DeadlineExceeded) || isNetErr(err) {
        time.Sleep(backoff(i))
        err = platform.SendImage(ctx, rctx, img)
    } else { break }
}

Prevention

When it happens

Trigger: Calling SendImage with valid context and data, but uploadWSMedia returns an error: WebSocket connection is down or closed, req_id response never arrives (timeout), totalChunks exceeds wecomWSUploadMaxChunks (100 x 512KB), or WeCom returns an error body in the upload finish frame.

Common situations: Sending large images near the 50MB chunk limit; unstable networks or the WeCom WS connection dropped mid-upload; calling SendImage before the platform finished connecting; WeCom rejecting the media (bad md5/base64 encoding or service-side limits).

Understand the failure class

Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/480aff0747607a7c. Report an issue: GitHub.