hyperledger/fabric · error
error marshaling ChaincodeEvent: proto: Marshal called with
Error message
error marshaling ChaincodeEvent: proto: Marshal called with nil
What it means
GetBytesChaincodeEvent serializes a *peer.ChaincodeEvent. When the passed pointer is nil, proto.Marshal cannot operate on it, so the function short-circuits and returns this explicit 'proto: Marshal called with nil' error. It exists to convert an otherwise opaque panic/failure into a clear message naming the offending message type.
Source
Thrown at protoutil/proputils.go:155
return nil, errors.New("error marshaling ChaincodeProposalPayload: proto: Marshal called with nil")
}
cppBytes, err := proto.Marshal(cpp)
return cppBytes, errors.Wrap(err, "error marshaling ChaincodeProposalPayload")
}
// GetBytesResponse gets the bytes of Response
func GetBytesResponse(res *peer.Response) ([]byte, error) {
if res == nil {
return nil, errors.New("error marshaling Response: proto: Marshal called with nil")
}
resBytes, err := proto.Marshal(res)
return resBytes, errors.Wrap(err, "error marshaling Response")
}
// GetBytesChaincodeEvent gets the bytes of ChaincodeEvent
func GetBytesChaincodeEvent(event *peer.ChaincodeEvent) ([]byte, error) {
if event == nil {
return nil, errors.New("error marshaling ChaincodeEvent: proto: Marshal called with nil")
}
eventBytes, err := proto.Marshal(event)
return eventBytes, errors.Wrap(err, "error marshaling ChaincodeEvent")
}
// GetBytesChaincodeActionPayload get the bytes of ChaincodeActionPayload from
// the message
func GetBytesChaincodeActionPayload(cap *peer.ChaincodeActionPayload) ([]byte, error) {
if cap == nil {
return nil, errors.New("error marshaling ChaincodeActionPayload: proto: Marshal called with nil")
}
capBytes, err := proto.Marshal(cap)
return capBytes, errors.Wrap(err, "error marshaling ChaincodeActionPayload")
}
// GetBytesProposalResponse gets proposal bytes response
func GetBytesProposalResponse(pr *peer.ProposalResponse) ([]byte, error) {
if pr == nil {View on GitHub (pinned to 2736b63f8f)
Solutions
- Nil-check the *peer.ChaincodeEvent before serialization and skip/return early when absent.
- In event-processing loops, treat nil events as 'no event emitted' rather than an error path.
- Ensure chaincode stub SetEvent was called if an event is expected.
- In tests, construct &peer.ChaincodeEvent{...} with required fields before calling.
Example fix
// before
evBytes, err := protoutil.GetBytesChaincodeEvent(evt)
// after
if evt == nil {
return nil // no event emitted for this transaction
}
evBytes, err := protoutil.GetBytesChaincodeEvent(evt) Defensive patterns
Strategy: type-guard
Validate before calling
if evt == nil {
return nil // no ChaincodeEvent emitted; skip serialization
} Type guard
func hasChaincodeAction(ca *peer.ChaincodeAction) bool {
return ca != nil
}
func hasEvent(tx *protoutil.TxMetadata) bool { /* check extracted event non-nil */ return false } Try / catch
evBytes, err := protoutil.GetBytesChaincodeEvent(evt)
if err != nil {
return errors.Wrap(err, "ChaincodeEvent serialization failed")
} Prevention
- Treat nil ChaincodeEvent as 'no event' in block/tx event processing loops.
- Only call SetEvent/emit logic when an event is required by the chaincode contract.
- Extract the event via protoutil helpers and check for nil before use.
When it happens
Trigger: Calling protoutil.GetBytesChaincodeEvent(nil), or passing an event field from a transaction/chaincode action where the event was absent (e.g. ccEvent not extracted from ChaincodeAction).
Common situations: Chaincode executed without firing an event but the listener code unconditionally serializes the event; parsing block events where some transactions legitimately carry no ChaincodeEvent.
Related errors
- error marshaling Response: proto: Marshal called with nil
- error marshaling ChaincodeActionPayload: proto: Marshal call
- error marshaling ProposalResponse: proto: Marshal called wit
- error marshaling Header: proto: Marshal called with nil
- invalid chaincode event
AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04).
Data as JSON: /api/errors/305c50ddcd0e9888.
Report an issue: GitHub.