chenhg5/cc-connect · error

weixin: empty file

Error message

weixin: empty file

What it means

Validation guard in Weixin SendFile: the file attachment's Data slice has zero length, so nothing can be uploaded; the caller produced an empty file attachment.

Source

Thrown at platform/weixin/media_outbound.go:197

			Media: &cdnMedia{
				EncryptQueryParam: ref.downloadParam,
				AESKey:            formatAesKeyForAPI(ref.aesKey),
				EncryptType:       1,
			},
			MidSize: ref.cipherSize,
		},
	}
	return p.sendSingleItem(ctx, rc, item)
}

// SendFile implements core.FileSender.
func (p *Platform) SendFile(ctx context.Context, replyCtx any, file core.FileAttachment) error {
	rc, err := p.resolveReplyContext(replyCtx)
	if err != nil {
		return err
	}
	if len(file.Data) == 0 {
		return fmt.Errorf("weixin: empty file")
	}
	name := strings.TrimSpace(file.FileName)
	if name == "" {
		name = "file.bin"
	}

	if isVideoFile(file) {
		ref, err := p.uploadToWeixinCDN(ctx, rc.peerUserID, file.Data, uploadMediaVideo, "SendFileVideo")
		if err != nil {
			return err
		}
		return p.sendSingleItem(ctx, rc, buildVideoMessageItem(ref))
	}

	ref, err := p.uploadToWeixinCDN(ctx, rc.peerUserID, file.Data, uploadMediaFile, "SendFile")
	if err != nil {
		return err
	}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Check len(file.Data) > 0 before calling SendFile.
  2. Verify the source file exists and is non-empty (os.Stat) before reading.
  3. Handle read errors so failures don't surface as empty data.

Example fix

// before
data, _ := os.ReadFile(path)
p.SendFile(ctx, rc, core.FileAttachment{FileName: name, Data: data}) // weixin: empty file
// after
data, err := os.ReadFile(path)
if err != nil || len(data) == 0 { return fmt.Errorf("read %s: %w", path, err) }
Defensive patterns

Strategy: validation

Validate before calling

if len(file.Data) == 0 { return errors.New("file attachment is empty; skipping send") }

Try / catch

if err := p.SendFile(ctx, rc, f); err != nil && strings.Contains(err.Error(), "empty file") { log.Error("empty file bytes for", "name", f.FileName) }

Prevention

When it happens

Trigger: Calling SendFile with core.FileAttachment{Data: nil} or an empty byte slice.

Common situations: os.ReadFile returned 0 bytes for an empty or unreadable-but-not-erroring file; caller constructed the attachment from a nil slice after a failed export.

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/1f5b0b07bc5b426f. Report an issue: GitHub.