Skip to main content

oxionnx_core/
error.rs

1//! Typed error returned by all public oxionnx methods.
2
3use std::fmt;
4
5/// Typed error returned by all public `Session` methods.
6#[derive(Debug)]
7pub enum OnnxError {
8    /// ONNX protobuf parsing or decoding failure.
9    Parse(String),
10    /// An operator referenced by the model is not in the registry.
11    UnknownOp(String),
12    /// Tensor dimensions are incompatible for the requested operation.
13    ShapeMismatch(String),
14    /// A required tensor (input or weight) was not found in the value map.
15    TensorNotFound(String),
16    /// A feature or data type is recognized but not yet implemented.
17    Unsupported(String),
18    /// An ONNX operator is recognized but not yet implemented.
19    UnsupportedOp(String),
20    /// The model structure is invalid (e.g. cyclic graph, missing outputs).
21    InvalidModel(String),
22    /// Catch-all for unexpected internal failures.
23    Internal(String),
24    /// Inference was cancelled via a `CancellationToken`.
25    Cancelled(String),
26    /// A tensor dtype is incompatible with the requested operation or dispatch path.
27    DTypeMismatch(String),
28    /// An arithmetic error occurred during computation (e.g. integer division by zero).
29    Arithmetic(String),
30}
31
32impl fmt::Display for OnnxError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::Parse(s) => write!(f, "Parse error: {s}"),
36            Self::UnknownOp(s) => write!(f, "Unknown op: {s}"),
37            Self::ShapeMismatch(s) => write!(f, "Shape mismatch: {s}"),
38            Self::TensorNotFound(s) => write!(f, "Tensor not found: {s}"),
39            Self::Unsupported(s) => write!(f, "Unsupported: {s}"),
40            Self::UnsupportedOp(s) => write!(f, "Unsupported op: {s}"),
41            Self::InvalidModel(s) => write!(f, "Invalid model: {s}"),
42            Self::Internal(s) => write!(f, "Internal error: {s}"),
43            Self::Cancelled(s) => write!(f, "Cancelled: {s}"),
44            Self::DTypeMismatch(s) => write!(f, "DType mismatch: {s}"),
45            Self::Arithmetic(s) => write!(f, "Arithmetic error: {s}"),
46        }
47    }
48}
49
50impl std::error::Error for OnnxError {}
51
52impl From<String> for OnnxError {
53    fn from(s: String) -> Self {
54        Self::Internal(s)
55    }
56}