{"record":{"id":"78322931514604e1","repo":"cloudflare/cloudflared","slug":"datagram-marshal-error-w","errorCode":null,"errorMessage":"datagram marshal error: %w","messagePattern":"datagram marshal error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"quic/v3/datagram_errors.go","lineNumber":23,"sourceCode":"\t\"fmt\"\n)\n\nvar (\n\tErrInvalidDatagramType                 error = errors.New(\"invalid datagram type expected\")\n\tErrDatagramHeaderTooSmall              error = fmt.Errorf(\"datagram should have at least %d byte\", datagramTypeLen)\n\tErrDatagramPayloadTooLarge             error = errors.New(\"payload length is too large to be bundled in datagram\")\n\tErrDatagramPayloadHeaderTooSmall       error = errors.New(\"payload length is too small to fit the datagram header\")\n\tErrDatagramPayloadInvalidSize          error = errors.New(\"datagram provided is an invalid size\")\n\tErrDatagramResponseMsgInvalidSize      error = errors.New(\"datagram response message is an invalid size\")\n\tErrDatagramResponseInvalidSize         error = errors.New(\"datagram response is an invalid size\")\n\tErrDatagramResponseMsgTooLargeMaximum  error = fmt.Errorf(\"datagram response error message length exceeds the length of the datagram maximum: %d\", maxResponseErrorMessageLen)\n\tErrDatagramResponseMsgTooLargeDatagram error = fmt.Errorf(\"datagram response error message length exceeds the length of the provided datagram\")\n\tErrDatagramICMPPayloadTooLarge         error = fmt.Errorf(\"datagram icmp payload exceeds %d bytes\", maxICMPPayloadLen)\n\tErrDatagramICMPPayloadMissing          error = errors.New(\"datagram icmp payload is missing\")\n)\n\nfunc wrapMarshalErr(err error) error {\n\treturn fmt.Errorf(\"datagram marshal error: %w\", err)\n}\n\nfunc wrapUnmarshalErr(err error) error {\n\treturn fmt.Errorf(\"datagram unmarshal error: %w\", err)\n}\n","sourceCodeStart":5,"sourceCodeEnd":29,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/quic/v3/datagram_errors.go#L5-L29","documentation":"wrapMarshalErr wraps any lower-level failure that occurs while serializing a datagram (payload or response message) in the QUIC v3 datagram layer. The library throws it to add a consistent 'datagram marshal error' prefix so callers can identify the serialization stage of failure. The wrapped cause is typically one of the ErrDatagram* sentinel errors, e.g. payload too large or missing ICMP payload.","triggerScenarios":"Calling MarshalBinary or MarshalPayloadHeaderTo on a datagram whose payload exceeds maxICMPPayloadLen, whose response message length exceeds the provided datagram capacity, or whose ICMP payload is missing.","commonSituations":"Sending oversized ICMP probe payloads through a tunnel datagram session; passing a destination buffer smaller than the serialized response; constructing a datagram struct without populating the payload field.","solutions":["Reduce the payload size to fit within maxICMPPayloadLen before marshaling.","Allocate a destination slice large enough for the serialized message (check ErrDatagramResponseMsgTooLargeDatagram).","Unwrap with errors.Is against the ErrDatagram* sentinels to identify the exact cause and handle each case.","Log the wrapped error (%w chain) to confirm which sentinel was returned."],"exampleFix":"// before\nif err := datagram.MarshalPayloadHeaderTo(buf); err != nil { return err }\n// after\nif err := datagram.MarshalPayloadHeaderTo(buf); err != nil {\n    if errors.Is(err, quicv3.ErrDatagramICMPPayloadTooLarge) {\n        return fmt.Errorf(\"icmp payload too large, max %d bytes\", quicv3.MaxICMPPayloadLen)\n    }\n    return err\n}","handlingStrategy":"try-catch","validationCode":"if len(payload) > maxICMPPayloadLen { return fmt.Errorf(\"payload too large: %d\", len(payload)) }","typeGuard":"func fitsInDatagram(payload []byte, buf []byte) bool { return len(payload) <= maxICMPPayloadLen && cap(buf) >= len(payload) }","tryCatchPattern":"if err := d.MarshalBinary(); err != nil {\n    switch {\n    case errors.Is(err, quicv3.ErrDatagramICMPPayloadTooLarge): // shrink payload\n    case errors.Is(err, quicv3.ErrDatagramResponseMsgTooLargeDatagram): // grow buffer\n    default: return err\n    }\n}","preventionTips":["Check payload sizes against maxICMPPayloadLen before sending","Pre-allocate destination buffers with sufficient capacity","Use errors.Is on wrapped sentinel errors","Add unit tests for oversized payloads"],"tags":["quic","datagram","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}