chenhg5/cc-connect · error

dingtalk: upload file: %w

Error message

dingtalk: upload file: %w

What it means

SendFile failed while uploading the file bytes to DingTalk's media upload API (uploadMedia), which returns the mediaID needed for the file message. The wrapped error carries the actual cause — upload HTTP failure, non-200 response, or token error.

Source

Thrown at platform/dingtalk/dingtalk.go:1113

	return p.createAICard(ctx, rc)
}

// SendFile uploads and sends a file via DingTalk oToMessages API.
// Implements core.FileSender.
func (p *Platform) SendFile(ctx context.Context, rctx any, file core.FileAttachment) error {
	rc, ok := rctx.(replyContext)
	if !ok {
		return fmt.Errorf("dingtalk: SendFile: invalid reply context type %T", rctx)
	}

	name := file.FileName
	if name == "" {
		name = "file"
	}

	mediaID, err := p.uploadMedia(ctx, file.Data, name, "file")
	if err != nil {
		return fmt.Errorf("dingtalk: upload file: %w", err)
	}

	slog.Debug("dingtalk: file uploaded", "media_id", mediaID, "name", name, "size", len(file.Data))

	token, err := p.getAccessToken()
	if err != nil {
		return fmt.Errorf("dingtalk: get access token: %w", err)
	}

	ext := ""
	if idx := strings.LastIndex(name, "."); idx >= 0 {
		ext = name[idx+1:]
	}

	msgParamBytes, _ := json.Marshal(map[string]string{
		"mediaId":  mediaID,
		"fileName": name,
		"fileType": ext,

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Read the wrapped error to identify the exact cause
  2. Check the file size against DingTalk's media upload limit and compress/split if needed
  3. Verify a valid access token is obtainable (check getAccessToken errors in logs)
  4. Test connectivity to api.dingtalk.com from the host
  5. Retry transient network failures with backoff

Example fix

// before
file.Data = hugeBuffer // 50MB
p.SendFile(ctx, rc, file)
// after
if len(file.Data) > 20*1024*1024 {
    return fmt.Errorf("file exceeds DingTalk media limit")
}
p.SendFile(ctx, rc, file)
Defensive patterns

Strategy: validation

Validate before calling

if len(file.Data) == 0 || len(file.Data) > 20*1024*1024 {
    return fmt.Errorf("file size %d outside DingTalk media upload limits", len(file.Data))
}

Try / catch

if err := p.SendFile(ctx, rc, file); err != nil {
    if strings.Contains(err.Error(), "upload file") {
        // uploadMedia failed; log wrapped cause and retry transient errors
        slog.Error("media upload failed", "err", err)
    }
    return err
}

Prevention

When it happens

Trigger: File too large for DingTalk media API limits; invalid mediaType; network failure during multipart upload; access token error inside uploadMedia; DingTalk rejecting the file name/content.

Common situations: Sending files > 20MB (DingTalk media limit); zero-byte or unreadable attachments; expired credentials; blocked network path to api.dingtalk.com.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


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