{"record":{"id":"87314be23197c418","repo":"hyperledger/fabric","slug":"failed-to-marshal-chaincode-event","errorCode":null,"errorMessage":"failed to marshal chaincode event","messagePattern":"failed to marshal chaincode event","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/endorser/endorser.go","lineNumber":416,"sourceCode":"\n\t\ttxParams.TXSimulator = txSim\n\t\ttxParams.HistoryQueryExecutor = hqe\n\t}\n\n\tcdLedger, err := e.Support.ChaincodeEndorsementInfo(up.ChannelID(), up.ChaincodeName, txParams.TXSimulator)\n\tif err != nil {\n\t\treturn nil, errors.WithMessagef(err, \"make sure the chaincode %s has been successfully defined on channel %s and try again\", up.ChaincodeName, up.ChannelID())\n\t}\n\n\t// 1 -- simulate\n\tres, simulationResult, ccevent, ccInterest, err := e.simulateProposal(txParams, up.ChaincodeName, up.Input)\n\tif err != nil {\n\t\treturn nil, errors.WithMessage(err, \"error in simulation\")\n\t}\n\n\tcceventBytes, err := CreateCCEventBytes(ccevent)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"failed to marshal chaincode event\")\n\t}\n\n\tprpBytes, err := protoutil.GetBytesProposalResponsePayload(up.ProposalHash, res, simulationResult, cceventBytes, &pb.ChaincodeID{\n\t\tName:    up.ChaincodeName,\n\t\tVersion: cdLedger.Version,\n\t})\n\tif err != nil {\n\t\tlogger.Warning(\"Failed marshaling the proposal response payload to bytes\", err)\n\t\treturn nil, errors.WithMessage(err, \"failed to create the proposal response\")\n\t}\n\n\t// if error, capture endorsement failure metric\n\tmeterLabels := []string{\n\t\t\"channel\", up.ChannelID(),\n\t\t\"chaincode\", up.ChaincodeName,\n\t}\n\n\tswitch {","sourceCodeStart":398,"sourceCodeEnd":434,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/endorser/endorser.go#L398-L434","documentation":"After successful simulation, the endorser serializes the chaincode event (CreateCCEventBytes) to embed in the proposal response. If that marshaling fails, the whole proposal processing fails with 'failed to marshal chaincode event' wrapping the underlying error.","triggerScenarios":"A chaincode sets a ChaincodeEvent whose payload cannot be marshaled (e.g. event produced during simulation is malformed), causing CreateCCEventBytes to return an error.","commonSituations":"Chaincode emitting very large or invalid event payloads; corrupted event objects from custom chaincode shim usage; binary/encoding edge cases in event payload bytes.","solutions":["Inspect the wrapped (cause) error to find why the event bytes failed to marshal","Fix the chaincode to emit a valid, reasonably sized event name/payload","Validate event payload bytes before SetEvent in chaincode"],"exampleFix":"// before\nstub.SetEvent(\"evt\", nil) // or invalid payload\n// after\nstub.SetEvent(\"evt\", []byte(\"valid-payload\"))","handlingStrategy":"try-catch","validationCode":"if ccevent != nil && len(ccevent.Payload) > maxEventSize {\n    return errors.New(\"chaincode event payload too large\")\n}","typeGuard":null,"tryCatchPattern":"cceventBytes, err := CreateCCEventBytes(ccevent)\nif err != nil {\n    return errors.Wrap(err, \"failed to marshal chaincode event\")\n}","preventionTips":["Emit small, valid event payloads from chaincode (SetEvent)","Inspect the wrapped cause with errors.Cause/Unwrap when this fires","Test event emission paths in chaincode unit tests"],"tags":["endorser","chaincode-event","serialization","marshal"],"backgroundTag":"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"}