{"record":{"id":"1599aa5c736324cb","repo":"nautechsystems/nautilus_trader","slug":"custom-data-type-type-name-is-already-register","errorCode":null,"errorMessage":"Custom data type \"{type_name}\" is already registered for JSON","messagePattern":"Custom data type \"(.+?)\" is already registered for JSON","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/data/registry.rs","lineNumber":75,"sourceCode":"        json: DashMap::new(),\n        #[cfg(feature = \"arrow\")]\n        arrow: DashMap::new(),\n    })\n}\n\n/// Registers a JSON deserializer for the given custom data type name.\n/// When `Data::deserialize` sees this type name, it will call this function.\n///\n/// # Errors\n/// Returns an error if the type is already registered.\npub fn register_json_deserializer(\n    type_name: &str,\n    deserializer: JsonDeserializer,\n) -> Result<(), anyhow::Error> {\n    let reg = registries();\n    match reg.json.entry(type_name.to_string()) {\n        Entry::Occupied(_) => {\n            anyhow::bail!(\"Custom data type \\\"{type_name}\\\" is already registered for JSON\");\n        }\n        Entry::Vacant(v) => {\n            v.insert(deserializer);\n            Ok(())\n        }\n    }\n}\n\n/// Registers a JSON deserializer for the given custom data type name if not already registered.\n/// If the type is already registered, returns `Ok(())` without overwriting (idempotent).\n/// Use this where repeated registration can occur (e.g. module init).\n///\n/// # Errors\n/// Does not return an error (idempotent insert into `DashMap`).\npub fn ensure_json_deserializer_registered(\n    type_name: &str,\n    deserializer: JsonDeserializer,\n) -> Result<(), anyhow::Error> {","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/data/registry.rs#L57-L93","documentation":"register_json_deserializer adds a JSON deserializer to the custom-data registry keyed by type name. The registry forbids duplicates, so registering the same type name twice bails with this error instead of silently overwriting the existing deserializer.","triggerScenarios":"Calling register_json_deserializer (or register_custom_data_json) twice for the same type_name, e.g. on module re-import, repeated adapter initialization, or two crates registering the same logical type.","commonSituations":"Python modules reloaded in notebooks or tests invoking registration each run; a library and application both registering the same custom type; hot-reloading adapters.","solutions":["Call registration only once, e.g. with a std::sync::Once / lazy static or an idempotent init guard.","Check first (or catch this error) and treat 'already registered' as success in idempotent init paths.","Use distinct type names if two genuinely different types collide."],"exampleFix":"// before\nregister_custom_data_json(\"MyData\", deser)?; // fails on second init\n// after\nstatic INIT: Once = Once::new();\nINIT.call_once(|| register_custom_data_json(\"MyData\", deser).unwrap());","handlingStrategy":"try-catch","validationCode":"// Rust — check before registering\n// if registry already contains key, skip (expose a lookup or maintain your own Once)","typeGuard":null,"tryCatchPattern":"// Rust\nif let Err(e) = register_json_deserializer(type_name, deser) {\n    if e.to_string().contains(\"already registered\") { Ok(()) } else { Err(e) } // idempotent\n} else { Ok(()) }","preventionTips":["Initialize registries once per process using OnceLock or a module-level flag.","In Python, put registration calls under `if not getattr(mod, '_registered', False)`.","Keep registration in a dedicated init function called from a single entry point."],"tags":["registry","duplicate-registration","custom-data"],"backgroundTag":"file-already-exists","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}