nautechsystems/nautilus_trader · critical

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

Error message

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

What it means

order_type_from_cstr converts a C string into an OrderType enum via FromStr. Unrecognized strings panic with this message, and abort_on_panic escalates the panic to a process abort at the FFI boundary. The library accepts only the exact variant strings emitted by OrderType's string conversions.

Source

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

pub extern "C" fn order_type_to_cstr(value: OrderType) -> *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 `OrderType` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn order_type_from_cstr(ptr: *const c_char) -> OrderType {
    abort_on_panic(|| {
        let value = unsafe { cstr_as_str(ptr) };
        OrderType::from_str(value)
            .unwrap_or_else(|_| panic!("invalid `OrderType` enum string value, was '{value}'"))
    })
}

#[unsafe(no_mangle)]
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)]

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Pass the exact OrderType variant string; verify against the '{value}' in the panic and the enum's FromStr definition.
  2. Translate venue-specific codes ('MKT'/'LMT') to canonical variant names in the caller before calling.
  3. Validate the string against the variant whitelist in the host language.
  4. Ensure the C string is exact and NUL-terminated with no stray whitespace.
  5. Rebuild the caller against the matching crate version if the enum vocabulary changed.

Example fix

// before
order_type_from_cstr("MKT"); // panics: venue code

// after
order_type_from_cstr("MARKET"); // canonical OrderType variant string
Defensive patterns

Strategy: validation

Validate before calling

const TYPE_MAP = { MKT: "MARKET", LMT: "LIMIT", STP: "STOP" };
const canonical = TYPE_MAP[raw] ?? raw;
const ORDER_TYPE_VARIANTS = new Set(["MARKET", "LIMIT", "STOP_MARKET", "STOP_LIMIT", "MARKET_TO_LIMIT", "MARKET_IF_TOUCHED", "LIMIT_IF_TOUCHED", "TRAILING_STOP_MARKET"]);
if (!ORDER_TYPE_VARIANTS.has(canonical)) throw new Error(`unsupported OrderType: ${raw}`);

Type guard

function isOrderType(v) { return typeof v === 'string' && ORDER_TYPE_VARIANTS.has(v); }

Try / catch

Not applicable: the panic aborts the process; cannot be caught from the host language. Translate venue codes and validate before order_type_from_cstr.

Prevention

When it happens

Trigger: Calling order_type_from_cstr(ptr) with strings like 'LIMIT_ORDER', 'MKT', 'StopLimit', 'bracket', or an empty string that do not match any OrderType variant. Only strings accepted by OrderStatus's sibling impl OrderType::from_str parse.

Common situations: Mapping venue order-type codes ('MKT', 'LMT', 'STP') directly into the FFI call without translation; building orders from config files with hand-written type names; case mismatches; version drift after enum renames.

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/dda9617705337ccd. Report an issue: GitHub.