JuliusBrussee/caveman · info

kms: encode request

Error message

kms: encode %s request: %w

What it means

call marshals the request body (map[string]string for encrypt/decrypt inputs) with json.Marshal and wraps failures as "kms: encode <operation> request: %w". Because the input is always a map[string]string, Marshal cannot fail in practice; this is a defensive guard on the request-building path of every KMS API call.

Solutions

  1. Treat as an internal invariant violation; retry and report if it reproduces.
  2. If you patched/forked the client, check that call inputs remain JSON-marshalable types.
  3. Inspect the wrapped inner error for the exact marshal failure cause.
Defensive patterns

Strategy: try-catch

Try / catch

err := client.Encrypt(ctx, plaintext)
if err != nil {
    if strings.Contains(err.Error(), "encode encrypt request") {
        return fmt.Errorf("internal: request marshal failed: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Practically never: json.Marshal of a map[string]string only fails on unencodable types, which this input type rules out. The branch exists to catch impossible states after future refactors change the input type.

Common situations: Not seen in normal use; would only surface if the library internals were modified to pass custom structs with unsupported field types (e.g. channels, funcs, or invalid MarshalJSON).

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/9c44df9d1eee83b5. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/kms/kms.go:349

		return errors.New("kms: probe plaintext mismatch")
	}
	return nil
}

func validateLocation(region, keyID string) error {
	if !regionPattern.MatchString(region) {
		return errors.New("kms: invalid Scaleway region")
	}
	if !keyIDPattern.MatchString(keyID) {
		return errors.New("kms: invalid Scaleway key ID")
	}
	return nil
}

func (c *Client) call(ctx context.Context, region, keyID, operation string, input, output any) error {
	body, err := json.Marshal(input)
	if err != nil {
		return fmt.Errorf("kms: encode %s request: %w", operation, err)
	}
	endpoint := c.apiBaseURL + "/key-manager/v1alpha1/regions/" + url.PathEscape(region) +
		"/keys/" + url.PathEscape(keyID) + "/" + operation
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
	if err != nil {
		return fmt.Errorf("kms: create %s request: %w", operation, err)
	}
	req.Header.Set("content-type", "application/json")
	req.Header.Set("accept", "application/json")
	req.Header.Set("x-auth-token", c.token)
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return fmt.Errorf("kms: %s request failed: %w", operation, err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 32<<10))
		return fmt.Errorf("kms: %s returned HTTP %d", operation, resp.StatusCode)

View on GitHub (pinned to 3ee70a1026)