vitessio/vitess · error

cannot marshal data: %v

Error message

cannot marshal data: %v

What it means

printJSON marshals the value with MarshalJSON; if json marshaling fails (unsupported types, channels, funcs, or cyclic data), the error is wrapped as 'cannot marshal data'. This is an internal serialization failure on the vtctl side, not a topo problem.

Source

Thrown at go/vt/vtctl/vtctl.go:3926

	}

	shardRanges, err := key.GenerateShardRanges(*numShards, 0)
	if err != nil {
		return err
	}

	return printJSON(wr.Logger(), shardRanges)
}

func commandPanic(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
	panic(errors.New("this command panics on purpose"))
}

// printJSON will print the JSON version of the structure to the logger.
func printJSON(logger logutil.Logger, val any) error {
	data, err := MarshalJSON(val)
	if err != nil {
		return fmt.Errorf("cannot marshal data: %v", err)
	}
	logger.Printf("%s\n", data)
	return nil
}

// loggerWriter turns a Logger into a Writer by decorating it with a Write()
// method that sends everything to Logger.Printf().
type loggerWriter struct {
	logutil.Logger
}

func (lw loggerWriter) Write(p []byte) (int, error) {
	lw.Printf("%s", p)
	return len(p), nil
}

// printQueryResult will pretty-print a QueryResult to the logger.
func printQueryResult(writer io.Writer, qr *sqltypes.Result) {

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Read the wrapped %v detail to identify which value failed to marshal
  2. Check the topo data for corrupted/unsupported field values in the object being printed
  3. If you develop vtctl, ensure custom types implement json.Marshaler or are converted before printJSON

Example fix

// before
return fmt.Errorf("cannot marshal data: %v", err)
// after
return vterrors.Wrapf(err, vtrpcpb.Code_INTERNAL, "cannot marshal data (%T)", val)
Defensive patterns

Strategy: try-catch

Try / catch

if err := printJSON(logger, val); err != nil {
	if strings.Contains(err.Error(), "cannot marshal data") {
		log.Warnf("falling back to %+v for %T", val, val)
		logger.Printf("%+v\n", val)
		return nil
	}
	return err
}

Prevention

When it happens

Trigger: Any vtctl command that prints its result via printJSON (e.g. GetSrvVSchema, GetPermissions) when the result value contains types json.Marshal cannot handle.

Common situations: MarshalJSON receiving a protobuf with an unexpected/invalid field type; corrupted topo data containing unsupported types; custom type lacking proper JSON encoding in a development build.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/2aebf7dfe5d85b0f. Report an issue: GitHub.