{"record":{"id":"005fc7ec2afed89a","repo":"micro/go-micro","slug":"ap2-marshal-mandate-w","errorCode":null,"errorMessage":"ap2: marshal mandate: %w","messagePattern":"ap2: marshal mandate: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gateway/a2a/ap2.go","lineNumber":146,"sourceCode":"\t\treturn out\n\t}\n\tif s.Mandate.Kind == AP2PaymentMandate && rail != nil {\n\t\tif s.Mandate.Rail == nil || *s.Mandate.Rail != *rail {\n\t\t\tout.Verified = false\n\t\t\tout.Error = \"ap2: settlement rail reference mismatch\"\n\t\t\treturn out\n\t\t}\n\t}\n\treturn out\n}\n\n// X402AP2Rail builds the x402 settlement rail reference carried under a payment mandate.\nfunc X402AP2Rail(reference string) AP2RailRef { return AP2RailRef{Type: \"x402\", Reference: reference} }\n\nfunc ap2Payload(m AP2Mandate) ([]byte, error) {\n\tb, err := json.Marshal(m)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"ap2: marshal mandate: %w\", err)\n\t}\n\tsum := sha256.Sum256(b)\n\treturn sum[:], nil\n}\n","sourceCodeStart":128,"sourceCodeEnd":151,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/gateway/a2a/ap2.go#L128-L151","documentation":"ap2Payload canonicalizes an AP2Mandate by JSON-marshaling it and hashing with SHA-256 to produce the bytes that are signed/verified. This error is returned if json.Marshal fails on the mandate — practically only when the mandate contains a value that cannot be marshaled (e.g. a channel, func, or cyclic structure in a custom field). It wraps the marshal error with %w and is reached from both SignAP2Mandate and VerifyAP2Mandate.","triggerScenarios":"Calling SignAP2Mandate or VerifyAP2Mandate with an AP2Mandate whose fields (or nested custom fields) contain un-marshalable Go values, causing 'ap2: marshal mandate: <json error>'.","commonSituations":"Embedding a func or channel in a custom/extension field of the mandate; a cyclic data structure in user metadata; using a custom MarshalJSON that itself errors.","solutions":["Inspect the wrapped json error to identify the offending field; remove or replace func/channel/cyclic values with marshalable data (strings, numbers).","Ensure any custom MarshalJSON on mandate sub-types cannot return an error for valid mandates.","Normalize metadata to plain JSON-safe types (map[string]string / encoding/json-compatible structs) before signing."],"exampleFix":"// before\nm := ap2.AP2Mandate{Meta: map[string]any{\"cb\": make(chan int)}}\n// after\nm := ap2.AP2Mandate{Meta: map[string]any{\"note\": \"hello\"}}","handlingStrategy":"validation","validationCode":"if _, err := json.Marshal(mandate); err != nil {\n    return fmt.Errorf(\"mandate not marshallable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"err := a2a.VerifyAP2Mandate(signed, pub)\nif err != nil && strings.Contains(err.Error(), \"marshal mandate\") {\n    // inspect the wrapped json error and fix the un-marshalable field\n}","preventionTips":["Keep mandate fields limited to JSON-safe types (strings, numbers, nested structs/maps).","Test SignAP2Mandate in CI with representative mandates to catch marshal issues early.","Ensure custom MarshalJSON implementations cannot error on valid data."],"tags":["go","ap2","json","marshaling","signature"],"backgroundTag":"json-marshal-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}