{"record":{"id":"9c44df9d1eee83b5","repo":"JuliusBrussee/caveman","slug":"kms-encode-s-request-w","errorCode":null,"errorMessage":"kms: encode %s request: %w","messagePattern":"kms: encode (.+?) request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"shared/platform/kms/kms.go","lineNumber":349,"sourceCode":"\t\treturn errors.New(\"kms: probe plaintext mismatch\")\n\t}\n\treturn nil\n}\n\nfunc validateLocation(region, keyID string) error {\n\tif !regionPattern.MatchString(region) {\n\t\treturn errors.New(\"kms: invalid Scaleway region\")\n\t}\n\tif !keyIDPattern.MatchString(keyID) {\n\t\treturn errors.New(\"kms: invalid Scaleway key ID\")\n\t}\n\treturn nil\n}\n\nfunc (c *Client) call(ctx context.Context, region, keyID, operation string, input, output any) error {\n\tbody, err := json.Marshal(input)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"kms: encode %s request: %w\", operation, err)\n\t}\n\tendpoint := c.apiBaseURL + \"/key-manager/v1alpha1/regions/\" + url.PathEscape(region) +\n\t\t\"/keys/\" + url.PathEscape(keyID) + \"/\" + operation\n\treq, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"kms: create %s request: %w\", operation, err)\n\t}\n\treq.Header.Set(\"content-type\", \"application/json\")\n\treq.Header.Set(\"accept\", \"application/json\")\n\treq.Header.Set(\"x-auth-token\", c.token)\n\tresp, err := c.httpClient.Do(req)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"kms: %s request failed: %w\", operation, err)\n\t}\n\tdefer resp.Body.Close()\n\tif resp.StatusCode != http.StatusOK {\n\t\t_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 32<<10))\n\t\treturn fmt.Errorf(\"kms: %s returned HTTP %d\", operation, resp.StatusCode)","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/kms/kms.go#L331-L367","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Treat as an internal invariant violation; retry and report if it reproduces.","If you patched/forked the client, check that call inputs remain JSON-marshalable types.","Inspect the wrapped inner error for the exact marshal failure cause."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"err := client.Encrypt(ctx, plaintext)\nif err != nil {\n    if strings.Contains(err.Error(), \"encode encrypt request\") {\n        return fmt.Errorf(\"internal: request marshal failed: %w\", err)\n    }\n    return err\n}","preventionTips":["Treat occurrences as an internal invariant violation.","Keep call() inputs as map[string]string or other always-marshalable types.","Add a fuzz/unit test asserting request marshaling never fails."],"tags":["go","kms","json","request"],"backgroundTag":"json-marshal-failed","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}