nautechsystems/nautilus_trader · critical

invalid `PositionSideOptional` enum string value, was '{valu

Error message

invalid `PositionSideOptional` enum string value, was '{value}'

What it means

position_side_from_cstr converts a C string into a PositionSideOptional enum via FromStr. If the string matches no variant the function panics with this message; abort_on_panic converts the panic into a process abort at the FFI boundary. This mirrors order_side_from_cstr but for position sides, including a none/flat variant.

Source

Thrown at crates/model/src/ffi/enums.rs:763

pub extern "C" fn position_side_to_cstr(value: PositionSideOptional) -> *const c_char {
    str_to_cstr(value.as_ref())
}

/// Returns an enum from a C string.
///
/// # Safety
///
/// Assumes `ptr` is a valid C string pointer.
///
/// # Panics
///
/// Panics if the C string does not correspond to a valid `PositionSideOptional` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn position_side_from_cstr(ptr: *const c_char) -> PositionSideOptional {
    abort_on_panic(|| {
        let value = unsafe { cstr_as_str(ptr) };
        PositionSideOptional::from_str(value).unwrap_or_else(|_| {
            panic!("invalid `PositionSideOptional` enum string value, was '{value}'")
        })
    })
}

#[unsafe(no_mangle)]
pub extern "C" fn position_adjustment_type_to_cstr(value: PositionAdjustmentType) -> *const c_char {
    str_to_cstr(value.as_ref())
}

/// Returns an enum from a C string.
///
/// # Safety
///
/// Assumes `ptr` is a valid C string pointer.
///
/// # Panics
///
/// Panics if the C string does not correspond to a valid `PositionAdjustmentType` variant.

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Pass the exact PositionSideOptional variant string (e.g. 'LONG', 'SHORT', or the none-variant string) per the enum's FromStr definition.
  2. Use the explicit none/flat variant string instead of an empty string for neutral positions.
  3. Map feed-specific direction vocabulary to canonical variants in the caller.
  4. Whitelist-validate the string before the FFI call.
  5. Rebuild against the matching crate version if variants were renamed.

Example fix

// before
position_side_from_cstr("short"); // panics: wrong case

// after
position_side_from_cstr("SHORT"); // exact PositionSideOptional variant string
Defensive patterns

Strategy: validation

Validate before calling

const POSITION_SIDE_OPTIONAL_VARIANTS = new Set(["LONG", "SHORT", "FLAT_OR_UNKNOWN"]);
function toPositionSideOptional(raw) {
  const v = raw == null || raw === "" ? "FLAT_OR_UNKNOWN" : raw;
  if (!POSITION_SIDE_OPTIONAL_VARIANTS.has(v)) throw new Error(`unsupported PositionSideOptional: ${raw}`);
  return v;
}

Type guard

function isPositionSideOptional(v) { return v === 'LONG' || v === 'SHORT' || v === 'FLAT_OR_UNKNOWN'; }

Try / catch

Not applicable: panics become process aborts via abort_on_panic; no host-language catch is possible. Normalize direction strings and map empties to the none variant before calling.

Prevention

When it happens

Trigger: Calling position_side_from_cstr(ptr) with strings like 'LONG_POSITION', 'short', '0', or an empty string that match no PositionSideOptional variant. Empty input does not map to the none variant — the explicit none-variant string must be used.

Common situations: Mapping broker position directions ('long'/'short', 'net long') directly into the FFI call; passing empty/None where the explicit none-variant string is required; case mismatches; version drift after a variant rename.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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