nautechsystems/nautilus_trader · critical

invalid `ContingencyTypeOptional` enum string value, was '{v

Error message

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

What it means

`contingency_type_from_cstr` converts a C string into the Rust `ContingencyTypeOptional` enum and panics when the string does not match a variant. Contingency types describe order relationships (OCO, OTO, etc.) plus the no-contingency case; only exact canonical serializations are accepted. The panic is routed through `abort_on_panic`, so invalid input at the FFI boundary aborts the process instead of returning an error.

Source

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

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

#[unsafe(no_mangle)]
pub extern "C" fn currency_type_to_cstr(value: CurrencyType) -> *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 `CurrencyType` variant.

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Print the offending string and compare with the canonical `ContingencyTypeOptional` variant strings.
  2. Construct values from the library enum and serialize with `contingency_type_to_cstr` instead of literals.
  3. Normalize case/whitespace; the parse is exact-match.
  4. Map broker-specific contingency codes to `ContingencyTypeOptional` explicitly in the adapter.
  5. Pre-validate the string against the accepted set before the FFI call to avoid a process abort.

Example fix

// before
ct = "OCO_ORDER"
contingency_type_from_cstr(ct.encode())
// after
ct = "OCO"  # verify exact canonical spelling via contingency_type_to_cstr
contingency_type_from_cstr(ct.encode())
Defensive patterns

Strategy: validation

Validate before calling

VALID_CONTINGENCY_TYPES = {"NO_CONTINGENCY", "OCO", "OTO", "OUO"}  # verify against ContingencyTypeOptional definition
assert ct_str in VALID_CONTINGENCY_TYPES, f"bad ContingencyTypeOptional: {ct_str!r}"
contingency_type_from_cstr(ct_str.encode())

Type guard

def is_valid_contingency_type(s: str) -> bool:
    return s in VALID_CONTINGENCY_TYPES  # canonical strings from ContingencyTypeOptional::as_ref

Prevention

When it happens

Trigger: Calling `contingency_type_from_cstr(ptr)` with an unrecognized contingency string (e.g. 'OCO_ORDER' where the canonical form differs), an empty string, or a value from a broker's own contingency vocabulary.

Common situations: Building bracket/OCO order payloads via the C API; adapters translating broker contingency fields; typos/case mismatches; serialization changes between library versions.

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