chenhg5/cc-connect · error

encode QR: %w

Error message

encode QR: %w

What it means

saveQRCodeImage wraps any failure from the qrcode encoder (rsc.io/qr) as "encode QR: %w". This happens before any file I/O, so it means the QR content itself could not be encoded at the requested M (medium) error-correction level. It indicates the data is too long or invalid for a QR code at level M.

Source

Thrown at cmd/cc-connect/feishu.go:705

		return
	}
	qrterminal.GenerateWithConfig(content, qrterminal.Config{
		Level:      qrterminal.M,
		Writer:     os.Stdout,
		HalfBlocks: false,
		BlackChar:  "██",
		WhiteChar:  "  ",
		QuietZone:  4,
	})
	if _, err := fmt.Fprintln(os.Stdout); err != nil {
		return
	}
}

func saveQRCodeImage(content, path string) error {
	code, err := qr.Encode(content, qr.M)
	if err != nil {
		return fmt.Errorf("encode QR: %w", err)
	}
	code.Scale = 8
	return os.WriteFile(path, code.PNG(), 0644)
}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Shorten the content being encoded: strip query parameters or use a shorter URL for the QR payload.
  2. Lower the error-correction level in saveQRCodeImage from qr.M to qr.L to increase capacity.
  3. Check the wrapped error (%w) to confirm it is an overflow vs. invalid input from qr.Encode.

Example fix

// before
code, err := qr.Encode(content, qr.M)
// after
code, err := qr.Encode(content, qr.L) // higher capacity, still scannable
Defensive patterns

Strategy: try-catch

Validate before calling

if len(content) > 2000 {
    return fmt.Errorf("QR content too long: %d bytes", len(content))
}

Try / catch

if err := saveQRCodeImage(content, path); err != nil {
    var encErr error
    if strings.Contains(err.Error(), "encode QR:") {
        encErr = fmt.Errorf("QR payload too large for level M: %w", err)
    }
    return encErr
}

Prevention

When it happens

Trigger: saveQRCodeImage(content, path) is called by runRegistrationFlow, runWeixinQRLoginFlow, and tests; qr.Encode returns an error when the content exceeds the maximum byte capacity of a QR code at error-correction level M, or the input cannot be encoded.

Common situations: A login/registration URL or token returned by Feishu/Weixin is unusually long (very long signed URLs, oversized tokens), pushing the payload past QR capacity at level M.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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