chenhg5/cc-connect · info

%s: CDN upload failed after %d attempts

Error message

%s: CDN upload failed after %d attempts

What it means

The degenerate branch of uploadBufferToCDN's post-loop return: reached only when the retry loop completes with lastErr == nil, which cannot normally happen (every non-success path sets lastErr). It returns the same 'failed after N attempts' message but without a wrapped cause.

Source

Thrown at platform/weixin/cdn.go:225

			if msg == "" {
				msg = fmt.Sprintf("status %d", resp.StatusCode)
			}
			lastErr = fmt.Errorf("%s: CDN upload server error: %s", label, msg)
			slog.Warn("weixin: CDN upload server error", "label", label, "attempt", attempt, "error", lastErr)
			continue
		}
		dl := resp.Header.Get("x-encrypted-param")
		if dl == "" {
			lastErr = fmt.Errorf("%s: CDN response missing x-encrypted-param", label)
			slog.Warn("weixin: CDN upload bad response", "label", label, "attempt", attempt)
			continue
		}
		return dl, nil
	}
	if lastErr != nil {
		return "", fmt.Errorf("%s: CDN upload failed after %d attempts: %w", label, cdnUploadMaxRetries, lastErr)
	}
	return "", fmt.Errorf("%s: CDN upload failed after %d attempts", label, cdnUploadMaxRetries)
}

func md5Hex(b []byte) string {
	h := md5.Sum(b)
	return hex.EncodeToString(h[:])
}

func detectImageMime(b []byte) string {
	if len(b) >= 3 && b[0] == 0xFF && b[1] == 0xD8 && b[2] == 0xFF {
		return "image/jpeg"
	}
	if len(b) >= 8 && string(b[0:8]) == "\x89PNG\r\n\x1a\n" {
		return "image/png"
	}
	if len(b) >= 6 && (string(b[0:6]) == "GIF87a" || string(b[0:6]) == "GIF89a") {
		return "image/gif"
	}
	if len(b) >= 12 && string(b[0:4]) == "RIFF" && string(b[8:12]) == "WEBP" {

View on GitHub (pinned to 4000b2338a)

Solutions

  1. If ever observed, treat it as an internal invariant violation and report it — inspect the loop logic in platform/weixin/cdn.go
  2. Add defensive logging/reporting around uploads so the absence of any attempt-level warning is visible
Defensive patterns

Strategy: fallback

Try / catch

if err != nil && strings.HasSuffix(err.Error(), "CDN upload failed after 3 attempts") && !strings.Contains(err.Error(), ": ") {
    // no wrapped cause: log as potential invariant violation with full context
}

Prevention

When it happens

Trigger: Logically unreachable; would require the retry loop to finish all 3 attempts without setting lastErr and without returning, e.g. if the success return were removed or the loop counter were 0.

Common situations: Effectively never seen in production; exists as a compile-safe final return for the compiler.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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