go-kratos/kratos · error

failed to marshal, message is %T, want proto.Message

Error message

failed to marshal, message is %T, want proto.Message

What it means

Error "failed to marshal, message is %T, want proto.Message" thrown in go-kratos/kratos.

Source

Thrown at transport/grpc/codec.go:25

	"google.golang.org/protobuf/proto"

	enc "github.com/go-kratos/kratos/v3/encoding"
	"github.com/go-kratos/kratos/v3/encoding/protojson"
)

const jsonName = "json"

func init() {
	encoding.RegisterCodec(codec{})
}

// codec is a Codec implementation with protobuf. It is the default codec for gRPC.
type codec struct{}

func (codec) Marshal(v any) ([]byte, error) {
	vv, ok := v.(proto.Message)
	if !ok {
		return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
	}
	return enc.GetCodec(protojson.Name).Marshal(vv)
}

func (codec) Unmarshal(data []byte, v any) error {
	vv, ok := v.(proto.Message)
	if !ok {
		return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
	}
	return enc.GetCodec(protojson.Name).Unmarshal(data, vv)
}

func (codec) Name() string {
	return jsonName
}

View on GitHub (pinned to 668db92c2c)

When it happens

Trigger: Thrown at transport/grpc/codec.go:25 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of go-kratos/kratos@668db92c2c (2026-08-16). Data as JSON: /api/errors/254d68d5d13bb36d. Report an issue: GitHub.