{"record":{"id":"26241d3fbac3b8fa","repo":"nautechsystems/nautilus_trader","slug":"failed-to-build-bybitswitchmodeparams","errorCode":null,"errorMessage":"Failed to build BybitSwitchModeParams","messagePattern":"Failed to build BybitSwitchModeParams","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/bybit/src/http/client.rs","lineNumber":1321,"sourceCode":"        mode: BybitPositionMode,\n        symbol: Option<String>,\n        coin: Option<String>,\n    ) -> Result<BybitSwitchModeResponse, BybitHttpError> {\n        let mut builder = BybitSwitchModeParamsBuilder::default();\n        builder.category(product_type);\n        builder.mode(mode);\n\n        if let Some(s) = symbol {\n            builder.symbol(s);\n        }\n\n        if let Some(c) = coin {\n            builder.coin(c);\n        }\n\n        let params = builder\n            .build()\n            .expect(\"Failed to build BybitSwitchModeParams\");\n\n        let body = serde_json::to_vec(&params)?;\n        self.send_request::<_, ()>(\n            Method::POST,\n            \"/v5/position/switch-mode\",\n            None,\n            Some(body),\n            true,\n        )\n        .await\n    }\n\n    /// Sets trading stop parameters including trailing stops.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if:\n    /// - Credentials are missing.","sourceCodeStart":1303,"sourceCodeEnd":1339,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/bybit/src/http/client.rs#L1303-L1339","documentation":"`switch_mode` builds `BybitSwitchModeParams` (category/symbol plus optional coin) and unwraps `build()` with `expect`. Since derive_builder `build()` only errs on missing required (no-default) fields, the panic means `category` or `symbol` was never set on the builder before `build()`.","triggerScenarios":"A code path reaching `switch_mode` where `builder.category(...)` or `builder.symbol(...)` was skipped — e.g. a refactor dropping a setter, or calling the raw builder without those fields.","commonSituations":"Directly using `BybitSwitchModeParamsBuilder` and forgetting `category`/`symbol`; merge conflicts that removed a setter line; internal code drift after API model changes.","solutions":["Ensure `.category(...)` and `.symbol(...)` are called on the builder before `.build()`","Replace `expect` with `?` error propagation returning `BybitHttpError`","If calling the public `switch_mode`, verify the category/symbol arguments are not being dropped before the builder","File a bug if reproducible through the public API"],"exampleFix":"// before\nlet params = builder.build().expect(\"Failed to build BybitSwitchModeParams\");\n// after\nlet params = builder.build().map_err(BybitHttpError::InvalidParams)?;","handlingStrategy":"validation","validationCode":"// category and symbol are required for switch_mode\nassert!(!symbol.is_empty(), \"symbol required for switch_mode\");","typeGuard":null,"tryCatchPattern":"client.switch_mode(cat, sym, coin).await.map_err(|e| { log::error!(\"switch_mode: {e}\"); e })?;","preventionTips":["Always supply both category and symbol when switching position mode","Close/reduce positions first: Bybit rejects mode switches with open positions (distinct API error)","Keep optional `coin` set for linear multi-asset mode when required"],"tags":["panic","builder","position-mode","bybit"],"backgroundTag":"missing-required-argument","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"}