chenhg5/cc-connect · error

build first-chunk request: %w

Error message

build first-chunk request: %w

What it means

Wraps http.NewRequestWithContext failure while building the Range bytes=0-0 probe request in resourceFetchFirstChunk. With a static URL template this essentially only fails on a malformed URL (e.g. invalid characters in messageID/fileKey or a bad domain), or a nil/invalid context. Note: when this fires, the caller resourceDownloadStream falls back to resourceSingleGet, which will also fail to build its request.

Source

Thrown at platform/feishu/resource_download.go:143

	// Server honoured Range. Loop the remaining chunks.
	if total > p.resourceMaxBytes {
		return nil, fmt.Errorf("resource too large: total=%d exceeds cap %d", total, p.resourceMaxBytes)
	}
	if int64(len(first)) >= total {
		// Defensive: a server that advertises 206 with first slice already
		// covering the whole resource is fine — return what we have.
		return first, nil
	}
	return p.resourceFetchRemainingChunks(ctx, token, messageID, fileKey, resType, total, first)
}

// resourceFetchFirstChunk issues Range bytes=0-0 to learn the total and grab
// the first byte. Returns (first, total, nil) where total==0 means the
// server ignored Range and the entire body is in `first`.
func (p *Platform) resourceFetchFirstChunk(ctx context.Context, token, messageID, fileKey, resType string) ([]byte, int64, error) {
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.resourceURL(messageID, fileKey, resType), nil)
	if err != nil {
		return nil, 0, fmt.Errorf("build first-chunk request: %w", err)
	}
	req.Header.Set("Authorization", "Bearer "+token)
	req.Header.Set("Range", "bytes=0-0")

	resp, err := p.resourceDownloadHTTP.Do(req)
	if err != nil {
		return nil, 0, fmt.Errorf("first-chunk request: %w", err)
	}
	defer func() { _, _ = io.Copy(io.Discard, resp.Body); _ = resp.Body.Close() }()

	switch resp.StatusCode {
	case http.StatusPartialContent:
		cr := resp.Header.Get("Content-Range")
		total, ok := parseContentRangeTotal(cr)
		if !ok {
			return nil, 0, fmt.Errorf("first-chunk: 206 without parseable Content-Range %q", cr)
		}
		body, err := io.ReadAll(io.LimitReader(resp.Body, 1))

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Validate the configured domain (open.feishu.cn / open.larksuite.com) for stray whitespace or invalid characters
  2. Ensure messageID/fileKey are validated (non-empty, no control characters) before download
  3. Check the wrapped error (%w) for the exact url.Parse message identifying the bad component

Example fix

// before
domain = "https://open.feishu.cn/ "
// after
domain = "https://open.feishu.cn"
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(fmt.Sprintf("%s/open-apis/im/v1/messages/%s/resources/%s", domain, msgID, key)); if err != nil { return err }

Type guard

null

Try / catch

if err != nil && strings.Contains(err.Error(), "build first-chunk request") {
    return fmt.Errorf("malformed resource URL (check domain/messageID/fileKey): %w", err)
}

Prevention

When it happens

Trigger: http.NewRequestWithContext returns an error, typically from url.Parse on p.resourceURL(...) output — e.g. a domain configured with invalid characters, or messageID/fileKey containing control characters that were not validated upstream.

Common situations: Misconfigured Feishu domain in config.toml (e.g. contains a space or newline), or a malformed message_id/file_key passed from an upstream envelope without validation.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


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