nautechsystems/nautilus_trader · error

Failed to build BybitSetMarginModeParams

Error message

Failed to build BybitSetMarginModeParams

What it means

`set_margin_mode` builds `BybitSetMarginModeParams` with derive_builder and unwraps `build()` with `expect`. `build()` fails only if a required (no-default) field is missing. The builder chain sets `set_margin_mode`, so any `Err` signals a mismatch between the struct's required fields and what the builder call chain sets — treated as an internal invariant violation and panicked.

Source

Thrown at crates/adapters/bybit/src/http/client.rs:1229

    /// - Credentials are missing.
    /// - The request fails.
    /// - The API returns an error.
    ///
    /// # Panics
    ///
    /// Panics if required parameters are not provided (should not happen with current implementation).
    ///
    /// # References
    ///
    /// - <https://bybit-exchange.github.io/docs/v5/account/set-margin-mode>
    pub async fn set_margin_mode(
        &self,
        margin_mode: BybitMarginMode,
    ) -> Result<BybitSetMarginModeResponse, BybitHttpError> {
        let params = BybitSetMarginModeParamsBuilder::default()
            .set_margin_mode(margin_mode)
            .build()
            .expect("Failed to build BybitSetMarginModeParams");

        let body = serde_json::to_vec(&params)?;
        self.send_request::<_, ()>(
            Method::POST,
            "/v5/account/set-margin-mode",
            None,
            Some(body),
            true,
        )
        .await
    }

    /// Sets leverage for a symbol.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Credentials are missing.

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check the `BybitSetMarginModeParams` struct definition for fields lacking defaults and ensure the builder chain sets all of them
  2. After upgrading the crate, compare this call site against the updated struct fields
  3. Replace the `expect` with `?` error propagation so misconfiguration returns `BybitHttpError`
  4. File a bug against the adapter if the panic occurs through the public `set_margin_mode`

Example fix

// before
let params = ...build().expect("Failed to build BybitSetMarginModeParams");
// after
let params = ...build().map_err(BybitHttpError::InvalidParams)?;
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the margin mode value is valid before the call
if !matches!(margin_mode, BybitMarginMode::Isolated | BybitMarginMode::Regular | BybitMarginMode::Portfolio) { return Err(MyError::InvalidMarginMode); }

Try / catch

match client.set_margin_mode(margin_mode).await { Err(BybitHttpError::Api{..}) => /* handle */, Err(e) => /* log & retry */, Ok(r) => r }

Prevention

When it happens

Trigger: `BybitSetMarginModeParams` gains a new required field that this call site does not set (e.g. after a Bybit V5 API update adding a mandatory parameter), making `build()` return `Err` and the `expect` panic.

Common situations: Upgrading the crate after a schema change where the builder chain was not updated; calling the raw builder without all required setters.

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


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/39fa5ba410837355. Report an issue: GitHub.