Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Avhz RustQuant Error

From Leeroopedia


Knowledge Sources
Domains Error_Handling, Quantitative_Finance
Last Updated 2026-02-07 19:00 GMT

Overview

Concrete tool for unified error handling across the RustQuant library.

Description

The RustQuantError enum, built with the thiserror crate, provides a comprehensive error type covering all error conditions across the library. It groups errors into several categories:

General errors: NotImplemented for unfinished features, ComputationError for runtime calculation failures, InvalidArgument for bad parameters, ConditionViolated for broken invariants, FileOperationFailed for I/O issues, MissingInput for absent required data, and MutexPoison for concurrent access failures.

Data errors: IoError (from std::io::Error) and PyO3Error (from pyo3::PyErr) for Python interop issues.

Statistical distribution errors: Dedicated variants wrapping errors from the rand_distr crate for Bernoulli, Binomial, ChiSquared, Exponential, Gamma, Gaussian (Normal), and Poisson distributions.

Linear regression errors: MatrixInversionFailed, SvdDecompositionFailed, and SvdDecompositionFailedOnU for numerical linear algebra failures.

Interpolation errors: UnequalLength for mismatched input arrays, Unfitted for uninitialized interpolators, OutsideOfRange for extrapolation attempts, and B-spline specific errors.

A separate CurveError enum handles curve-specific errors (DateOutsideRange, NoPoints). The module also provides an error! macro for convenient error creation and implements From<RustQuantError> for pyo3::PyErr for seamless Python error propagation.

Usage

Use RustQuantError as the standard error type for any fallible operation within RustQuant, enabling consistent error propagation with the ? operator across all library modules.

Code Reference

Source Location

Signature

#[derive(Debug, Error)]
pub enum RustQuantError {
    NotImplemented(String),
    ComputationError(String),
    InvalidArgument(String),
    ConditionViolated(String),
    FileOperationFailed(String),
    MissingInput(String),
    MutexPoison(#[from] std::sync::PoisonError<()>),
    IoError(#[from] std::io::Error),
    PyO3Error(#[from] pyo3::PyErr),
    Bernoulli(#[from] rand_distr::BernoulliError),
    Binomial(#[from] rand_distr::BinomialError),
    ChiSquared(#[from] rand_distr::ChiSquaredError),
    Exponential(#[from] rand_distr::ExpError),
    Gamma(#[from] rand_distr::GammaError),
    Gaussian(#[from] rand_distr::NormalError),
    Poisson(#[from] rand_distr::PoissonError),
    MatrixInversionFailed,
    SvdDecompositionFailed,
    SvdDecompositionFailedOnU,
    UnequalLength,
    Unfitted,
    OutsideOfRange,
    BSplineInvalidParameters(usize, usize, usize),
    BSplineOutsideOfRange(String),
}

#[derive(Debug, Clone, Copy)]
pub enum CurveError {
    DateOutsideRange,
    NoPoints,
}

macro_rules! error { ... }

Import

use RustQuant::error::RustQuantError;

I/O Contract

Inputs

Name Type Required Description
message String For string variants Descriptive error message
source error various For #[from] variants Upstream error automatically converted via From trait

Outputs

Name Type Description
RustQuantError enum A typed error with Display formatting for user-facing messages
pyo3::PyErr From conversion Automatic conversion to Python ValueError for PyO3 bindings

Usage Examples

use RustQuant::error::RustQuantError;

// Return a computation error
fn risky_calculation(x: f64) -> Result<f64, RustQuantError> {
    if x < 0.0 {
        return Err(RustQuantError::InvalidArgument(
            "Input must be non-negative".to_string()
        ));
    }
    Ok(x.sqrt())
}

// Use the error! macro for concise error creation
// return Err(error!(ComputationError, "SVD failed"));

// Errors from std::io and rand_distr auto-convert via From
fn read_data(path: &str) -> Result<String, RustQuantError> {
    let data = std::fs::read_to_string(path)?; // IoError auto-converts
    Ok(data)
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment