{"record":{"id":"2aebf7dfe5d85b0f","repo":"vitessio/vitess","slug":"cannot-marshal-data-v","errorCode":null,"errorMessage":"cannot marshal data: %v","messagePattern":"cannot marshal data: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vtctl/vtctl.go","lineNumber":3926,"sourceCode":"\t}\n\n\tshardRanges, err := key.GenerateShardRanges(*numShards, 0)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn printJSON(wr.Logger(), shardRanges)\n}\n\nfunc commandPanic(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {\n\tpanic(errors.New(\"this command panics on purpose\"))\n}\n\n// printJSON will print the JSON version of the structure to the logger.\nfunc printJSON(logger logutil.Logger, val any) error {\n\tdata, err := MarshalJSON(val)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"cannot marshal data: %v\", err)\n\t}\n\tlogger.Printf(\"%s\\n\", data)\n\treturn nil\n}\n\n// loggerWriter turns a Logger into a Writer by decorating it with a Write()\n// method that sends everything to Logger.Printf().\ntype loggerWriter struct {\n\tlogutil.Logger\n}\n\nfunc (lw loggerWriter) Write(p []byte) (int, error) {\n\tlw.Printf(\"%s\", p)\n\treturn len(p), nil\n}\n\n// printQueryResult will pretty-print a QueryResult to the logger.\nfunc printQueryResult(writer io.Writer, qr *sqltypes.Result) {","sourceCodeStart":3908,"sourceCodeEnd":3944,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtctl/vtctl.go#L3908-L3944","documentation":"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.","triggerScenarios":"Any vtctl command that prints its result via printJSON (e.g. GetSrvVSchema, GetPermissions) when the result value contains types json.Marshal cannot handle.","commonSituations":"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.","solutions":["Read the wrapped %v detail to identify which value failed to marshal","Check the topo data for corrupted/unsupported field values in the object being printed","If you develop vtctl, ensure custom types implement json.Marshaler or are converted before printJSON"],"exampleFix":"// before\nreturn fmt.Errorf(\"cannot marshal data: %v\", err)\n// after\nreturn vterrors.Wrapf(err, vtrpcpb.Code_INTERNAL, \"cannot marshal data (%T)\", val)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := printJSON(logger, val); err != nil {\n\tif strings.Contains(err.Error(), \"cannot marshal data\") {\n\t\tlog.Warnf(\"falling back to %+v for %T\", val, val)\n\t\tlogger.Printf(\"%+v\\n\", val)\n\t\treturn nil\n\t}\n\treturn err\n}","preventionTips":["Ensure all types routed to printJSON are JSON-safe (no channels/funcs/cycles)","Fuzz vtctl print paths with representative topo data","Wrap custom types with json.Marshaler implementations"],"tags":["vtctl","json","serialization","go"],"backgroundTag":"json-marshal-failed","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}