cloudflare/cloudflared · error

datagram response message is an invalid size

Error message

datagram response message is an invalid size

What it means

ErrDatagramResponseMsgInvalidSize is returned by UDPSessionRegistrationResponseDatagram.MarshalBinary when the ErrorMsg field exceeds maxResponseErrorMessageLen. The response datagram format reserves a fixed-size field for the error message, so longer messages cannot be encoded. The library refuses to marshal rather than silently truncating the message.

Source

Thrown at quic/v3/datagram_errors.go:14

package v3

import (
	"errors"
	"fmt"
)

var (
	ErrInvalidDatagramType                 error = errors.New("invalid datagram type expected")
	ErrDatagramHeaderTooSmall              error = fmt.Errorf("datagram should have at least %d byte", datagramTypeLen)
	ErrDatagramPayloadTooLarge             error = errors.New("payload length is too large to be bundled in datagram")
	ErrDatagramPayloadHeaderTooSmall       error = errors.New("payload length is too small to fit the datagram header")
	ErrDatagramPayloadInvalidSize          error = errors.New("datagram provided is an invalid size")
	ErrDatagramResponseMsgInvalidSize      error = errors.New("datagram response message is an invalid size")
	ErrDatagramResponseInvalidSize         error = errors.New("datagram response is an invalid size")
	ErrDatagramResponseMsgTooLargeMaximum  error = fmt.Errorf("datagram response error message length exceeds the length of the datagram maximum: %d", maxResponseErrorMessageLen)
	ErrDatagramResponseMsgTooLargeDatagram error = fmt.Errorf("datagram response error message length exceeds the length of the provided datagram")
	ErrDatagramICMPPayloadTooLarge         error = fmt.Errorf("datagram icmp payload exceeds %d bytes", maxICMPPayloadLen)
	ErrDatagramICMPPayloadMissing          error = errors.New("datagram icmp payload is missing")
)

func wrapMarshalErr(err error) error {
	return fmt.Errorf("datagram marshal error: %w", err)
}

func wrapUnmarshalErr(err error) error {
	return fmt.Errorf("datagram unmarshal error: %w", err)
}

View on GitHub (pinned to 2253eeeb25)

Solutions

  1. Truncate ErrorMsg to maxResponseErrorMessageLen bytes before setting it on the datagram
  2. Replace long error text with a short reason code and log the full message locally instead
  3. Use errors.Is(err, v3.ErrDatagramResponseMsgInvalidSize) after MarshalBinary to catch and shorten the message

Example fix

// before
resp := &v3.UDPSessionRegistrationResponseDatagram{ErrorMsg: veryLongErrorString}
data, err := resp.MarshalBinary() // -> ErrDatagramResponseMsgInvalidSize
// after
msg := veryLongErrorString
if len(msg) > maxResponseErrorMessageLen {
    msg = msg[:maxResponseErrorMessageLen]
}
resp := &v3.UDPSessionRegistrationResponseDatagram{ErrorMsg: msg}
data, err := resp.MarshalBinary()
Defensive patterns

Strategy: validation

Validate before calling

if len(errMsg) > maxResponseErrorMessageLen {
    errMsg = errMsg[:maxResponseErrorMessageLen]
}

Try / catch

if _, err := resp.MarshalBinary(); errors.Is(err, v3.ErrDatagramResponseMsgInvalidSize) {
    resp.ErrorMsg = "error details too long"
    _, err = resp.MarshalBinary()
}

Prevention

When it happens

Trigger: Marshaling a UDPSessionRegistrationResponseDatagram whose ErrorMsg string is longer than maxResponseErrorMessageLen bytes.

Common situations: Propagating raw origin/registration errors (long Go error strings, wrapped chain text) into the response ErrorMsg without trimming; embedding stack traces or URLs in ErrorMsg.

Understand the failure class

Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.

Related errors


AI-assisted analysis of cloudflare/cloudflared@2253eeeb25 (2026-09-06). Data as JSON: /api/errors/39f1448bdc36dd3d. Report an issue: GitHub.