{"record":{"id":"af533d230107fbbb","repo":"prestodb/presto","slug":"type-error-classname-typename-fieldname","errorCode":null,"errorMessage":"{type_error} {className} {typeName} {fieldName}","messagePattern":"\\{type_error\\} \\{className\\} \\{typeName\\} \\{fieldName\\}","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"presto-native-execution/presto_cpp/presto_protocol/core/presto_protocol_core.h","lineNumber":156,"sourceCode":"template <typename T>\nvoid to_json_key(json& j, const char* key, const std::shared_ptr<T>& value) {\n  if (value != nullptr) {\n    j[key] = value;\n  }\n}\n\ntemplate <typename T>\nvoid to_json_key(\n    json& j,\n    const char* key,\n    const T& value,\n    const char* className,\n    const char* typeName,\n    const char* fieldName) {\n  try {\n    to_json_key(j, key, value);\n  } catch (json::type_error& e) {\n    throw TypeError(\n        std::string(e.what()) + \" \" + className + \" \" + typeName + \" \" +\n        fieldName);\n  }\n}\n\ntemplate <typename T>\nvoid from_json_key(const json& j, const char* key, T& value) {\n  j.at(key).get_to(value);\n}\n\ntemplate <typename T>\nvoid from_json_key(const json& j, const char* key, std::shared_ptr<T>& value) {\n  if (j.count(key)) {\n    j.at(key).get_to(value);\n  }\n}\n\ntemplate <typename T>","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-native-execution/presto_cpp/presto_protocol/core/presto_protocol_core.h#L138-L174","documentation":"This TypeError is thrown by the keyed-serialization helper when to_json_key raises a json::type_error while serializing a map/keyed field. It is re-thrown as TypeError with the class name, type name, and field name appended, so you know exactly which protocol class/field failed to serialize. Typically the key or value type does not match what nlohmann json expects (e.g. serializing a non-string key, or a value whose to_json overload is missing/mismatched).","triggerScenarios":"Calling to_json on a protocol object with a keyed field (map-like) where the key or value cannot be converted by nlohmann json — e.g. wrong variant alternative, undefined json value assigned where an object is required, or key type not convertible to string.","commonSituations":"Adding a new field of an unsupported type to a protocol struct; a custom type lacking a to_json overload; nullptr/empty optional being serialized as json where an object is expected.","solutions":["Read the appended className/typeName/fieldName in the message to identify the failing field","Add or fix the to_json/to_json_key overload for the field's type","Ensure keys are std::string-convertible and values have valid serializers","Test serialization of the struct in a unit test with representative data"],"exampleFix":"// before: field without serializer\nstruct MyParams { std::chrono::milliseconds ttl; };\n// after: provide conversion\ninline void to_json(json& j, const MyParams& p) { j = json{{\"ttl\", p.ttl.count()}}; }","handlingStrategy":"try-catch","validationCode":"// serialize a sample instance before sending\njson probe;\nto_json(probe, myProtocolObject); // fails fast on missing serializers","typeGuard":"bool serializable(const json& j) {\n  return !j.is_null() || /* field optional */ true;\n}","tryCatchPattern":"try {\n  to_json(j, obj);\n} catch (const TypeError& e) {\n  // message carries className typeName fieldName\n  LOG(ERROR) << \"serialization failed: \" << e.what();\n  throw;\n}","preventionTips":["Provide to_json overloads for every field type in protocol structs","Keep map keys std::string-convertible","Add round-trip (to_json -> from_json) unit tests for protocol structs","Never serialize null where an object is required"],"tags":["json","serialization","type-error","presto-protocol"],"backgroundTag":"json-type-mismatch","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}