{"record":{"id":"e1aa8591403cf096","repo":"hyperledger/fabric","slug":"failed-to-marshal-output","errorCode":null,"errorMessage":"failed to marshal output","messagePattern":"failed to marshal output","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/peer/lifecycle/chaincode/common.go","lineNumber":130,"sourceCode":"\tif collectionsConfigFile != \"\" {\n\t\tvar err error\n\t\tccp, _, err = chaincode.GetCollectionConfigFromFile(collectionsConfigFile)\n\t\tif err != nil {\n\t\t\treturn nil, errors.WithMessagef(err, \"invalid collection configuration in file %s\", collectionsConfigFile)\n\t\t}\n\t}\n\treturn ccp, nil\n}\n\nfunc printResponseAsJSON(proposalResponse *pb.ProposalResponse, msg proto.Message, out io.Writer) error {\n\terr := proto.Unmarshal(proposalResponse.Response.Payload, msg)\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to unmarshal proposal response's response payload as type %T\", msg)\n\t}\n\n\tbytes, err := json.MarshalIndent(msg, \"\", \"\\t\")\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"failed to marshal output\")\n\t}\n\n\tfmt.Fprintf(out, \"%s\\n\", string(bytes))\n\n\treturn nil\n}\n","sourceCodeStart":112,"sourceCodeEnd":137,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/internal/peer/lifecycle/chaincode/common.go#L112-L137","documentation":"After successfully unmarshaling the response payload, printResponseAsJSON re-serializes the message with json.MarshalIndent for human-readable output. If JSON marshaling fails (rare for protobuf-backed messages), the error is wrapped as \"failed to marshal output\".","triggerScenarios":"printResponseAsJSON receives a msg whose JSON conversion fails — e.g. a message containing unsupported/invalid values (NaN-like fields, custom types without JSON support).","commonSituations":"Extremely rare with stock fabric messages; can appear in forks that pass non-standard proto messages or when jsonpb marshaling options conflict.","solutions":["Inspect the wrapped json error to find the offending field/type","Use protojson (jsonpb) marshaling for protobuf messages in forked code","Upgrade/patch the fork to use standard lifecycle result messages"],"exampleFix":"// before\nbytes, err := json.MarshalIndent(msg, \"\", \"\\t\")\n// after\nbytes, err := protojson.MarshalOptions{Multiline: true, Indent: \"\\t\"}.Marshal(msg)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := printResponseAsJSON(resp, msg, out); err != nil {\n    if strings.Contains(err.Error(), \"failed to marshal output\") {\n        // fall back to printing the raw payload\n    }\n    return err\n}","preventionTips":["Use protojson for protobuf-to-JSON conversion in forked code","Avoid injecting custom field types into lifecycle result messages","Pin fabric versions when patching response handling"],"tags":["fabric","json","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}