chenhg5/cc-connect · error

empty media data

Error message

empty media data

What it means

uploadWSMedia validates the media payload before starting a chunked upload to WeCom (WeChat Work) over the WebSocket transport. It computes totalChunks = ceil(len(data)/wecomWSUploadChunkSize); when the caller passes a zero-length byte slice there are no chunks to upload, so it refuses to initiate the aibot_upload_media_init handshake and returns this error instead of sending a doomed request. It exists to fail fast with a clear message rather than produce a confusing server-side failure.

Source

Thrown at platform/wecom/websocket_outbound_media.go:47

	}
	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")
	initFrame := map[string]any{
		"cmd":     "aibot_upload_media_init",
		"headers": map[string]string{"req_id": initReqID},
		"body": map[string]any{
			"type":         mediaType,
			"filename":     filename,
			"total_size":   len(data),
			"total_chunks": totalChunks,
			"md5":          hex.EncodeToString(sum[:]),
		},
	}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Check len(data) > 0 (or that the file size is non-zero) before calling SendImage/SendFile and surface a clearer upstream error.
  2. If the bytes come from a file, verify the file exists and is non-empty (os.Stat size > 0) and re-generate or re-download it if truncated.
  3. If the bytes come from another function, fix that function to return an error instead of empty bytes on failure, then propagate it.
  4. For unavoidable empties, skip the send (reply with a text message) rather than attempting media upload.

Example fix

// before
if err := p.SendImage(chatID, data, "photo.png"); err != nil { ... } // panics later with "empty media data"

// after
if len(data) == 0 {
    return fmt.Errorf("wecom: image data is empty, file may be corrupt or missing")
}
if err := p.SendImage(chatID, data, "photo.png"); err != nil { ... }
Defensive patterns

Strategy: validation

Validate before calling

func canUpload(data []byte) error {
    if len(data) == 0 {
        return errors.New("media data is empty; source file may be missing or corrupt")
    }
    return nil
}
// call before SendImage/SendFile: if err := canUpload(data); err != nil { return err }

Type guard

func hasMediaData(data []byte) bool { return len(data) > 0 }

Try / catch

var err *EmptyMediaError
if errors.As(err2, &err) {
    // regenerate or skip the media item, log with context
}

Prevention

When it happens

Trigger: Calling WSPlatform.SendImage or SendFile (both call uploadWSMedia) with an empty data slice: e.g. os.ReadFile returning a 0-byte file, an HTTP download that yielded no bytes, or a nil/empty []byte passed programmatically.

Common situations: Reading an image/file from disk that is unexpectedly 0 bytes (truncated download, failed export, race where the writer hasn't flushed yet); a media-producing pipeline that swallows an upstream error and returns nil bytes; tests constructing SendImage calls without fixture data.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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