nautechsystems/nautilus_trader · error
Failed to build BybitSetLeverageParams
Error message
Failed to build BybitSetLeverageParams
What it means
`set_leverage` builds `BybitSetLeverageParams` via derive_builder and panics through `expect` if `build()` fails, i.e. if a required field without a default is missing. The chain sets category, symbol, buy_leverage and sell_leverage; any remaining required field unset causes the panic.
Source
Thrown at crates/adapters/bybit/src/http/client.rs:1271
/// Panics if required parameters are not provided (should not happen with current implementation).
///
/// # References
///
/// - <https://bybit-exchange.github.io/docs/v5/position/leverage>
pub async fn set_leverage(
&self,
product_type: BybitProductType,
symbol: &str,
buy_leverage: &str,
sell_leverage: &str,
) -> Result<BybitSetLeverageResponse, BybitHttpError> {
let params = BybitSetLeverageParamsBuilder::default()
.category(product_type)
.symbol(symbol.to_string())
.buy_leverage(buy_leverage.to_string())
.sell_leverage(sell_leverage.to_string())
.build()
.expect("Failed to build BybitSetLeverageParams");
let body = serde_json::to_vec(¶ms)?;
self.send_request::<_, ()>(
Method::POST,
"/v5/position/set-leverage",
None,
Some(body),
true,
)
.await
}
/// Switches position mode for a product type.
///
/// # Errors
///
/// Returns an error if:
/// - Credentials are missing.View on GitHub (pinned to 18893faf8b)
Solutions
- Inspect `BybitSetLeverageParams` for non-optional fields and add the missing setter to the chain
- After a crate upgrade, rebuild and address any newly required builder fields
- Convert the `expect` to `?`/error mapping for graceful failure
- Report a bug if it panics via the public `set_leverage` path
Example fix
// before
.build().expect("Failed to build BybitSetLeverageParams");
// after
.build().map_err(BybitHttpError::InvalidParams)?; Defensive patterns
Strategy: validation
Validate before calling
// Validate leverage strings are numeric and within Bybit limits before calling let b: f64 = buy_leverage.parse()?; let s: f64 = sell_leverage.parse()?; assert!((1.0..=100.0).contains(&b) && (1.0..=100.0).contains(&s), "leverage out of range");
Try / catch
if let Err(e) = client.set_leverage(cat, sym, buy, sell).await { log::error!("set_leverage failed: {e}"); return Err(e.into()); } Prevention
- Format leverage as strings like "10" matching Bybit step rules
- Ensure buy/sell leverage are equal unless using Bybit cross-margin semantics
- Re-check builder field requirements after dependency upgrades
When it happens
Trigger: A newly added required field on `BybitSetLeverageParams` (e.g. a Bybit V5 API addition like `borrow_mode`) not set by this builder chain, so `build()` returns `Err` and the `expect` fires.
Common situations: Crate upgrade after Bybit adds a mandatory leverage parameter; constructing the builder manually and omitting a required setter.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- Failed to build BybitOpenOrdersParams
- Failed to build BybitSetMarginModeParams
- Failed to build BybitSwitchModeParams
- Failed to build BybitBorrowParams
- Failed to build BybitNoConvertRepayParams
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/415a4ce2cfe0dc65.
Report an issue: GitHub.