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
- Check len(data) > 0 (or that the file size is non-zero) before calling SendImage/SendFile and surface a clearer upstream error.
- 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.
- If the bytes come from another function, fix that function to return an error instead of empty bytes on failure, then propagate it.
- 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
- Always os.Stat the source file and check Size() > 0 before reading and sending.
- Make every media-producing function return (data, error) and treat nil data + nil error as a bug.
- Add a unit test asserting SendImage rejects empty data with a clear message.
- Validate media immediately after generation (download/encode), not at send time.
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
- media too large: %d chunks exceeds maximum %d
- empty attachment data
- wecom-ws: bot_id and bot_secret are required for websocket m
- wecom-ws: invalid aeskey base64 length
- wecom-ws: image data is empty
AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06).
Data as JSON: /api/errors/6df817218022f646.
Report an issue: GitHub.