chenhg5/cc-connect · error

wecom-ws: file data is empty

Error message

wecom-ws: file data is empty

What it means

JSON parse failure on the response of POST /cgi-bin/message/send for an image message: the HTTP call itself succeeded but the body is not valid JSON matching the {errcode, errmsg} shape. Typically the endpoint returned an HTML error page (gateway/proxy issue), an auth redirect, or a truncated body instead of the expected API envelope.

Source

Thrown at platform/wecom/websocket_outbound_media.go:168

		return "image.gif"
	case "image/webp":
		return "image.webp"
	default:
		return "image.png"
	}
}

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

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

func wsFileFileName(file core.FileAttachment) string {
	name := filepath.Base(strings.TrimSpace(file.FileName))
	if name != "" && name != "." {
		return name
	}
	switch strings.ToLower(file.MimeType) {

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Load the file bytes into file.Data before calling SendFile (e.g. os.ReadFile into Data)
  2. Check len(file.Data) at the call site and skip/queue empty attachments
  3. Verify the upstream read/download that populated Data actually succeeded

Example fix

// before
file := core.FileAttachment{FileName: "report.pdf"} // Data empty
p.SendFile(ctx, rc, file)
// after
data, err := os.ReadFile("report.pdf")
if err != nil {
	return err
}
file := core.FileAttachment{FileName: "report.pdf", Data: data}
p.SendFile(ctx, rc, file)
Defensive patterns

Strategy: validation

Validate before calling

if len(file.Data) == 0 {
	return fmt.Errorf("attachment %s has no data; load bytes before SendFile", file.FileName)
}

Try / catch

if err := p.SendFile(ctx, rc, file); err != nil && strings.Contains(err.Error(), "file data is empty") {
	log.Warn("empty attachment skipped", "name", file.FileName)
}

Prevention

When it happens

Trigger: Called with core.FileAttachment{...} where Data is nil or an empty slice — e.g. a failed prior file read, or an attachment constructed with only FileName/Path but never loaded.

Common situations: Caller passed a path expecting the platform to read it (it does not — Data must be preloaded); the file was read with an error that was ignored; attachment produced from an empty download.

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/4ffca0b45346e08c. Report an issue: GitHub.