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`](crate::CancellationToken).
25    Cancelled(String),
26}
27
28impl fmt::Display for OnnxError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            Self::Parse(s) => write!(f, "Parse error: {s}"),
32            Self::UnknownOp(s) => write!(f, "Unknown op: {s}"),
33            Self::ShapeMismatch(s) => write!(f, "Shape mismatch: {s}"),
34            Self::TensorNotFound(s) => write!(f, "Tensor not found: {s}"),
35            Self::Unsupported(s) => write!(f, "Unsupported: {s}"),
36            Self::UnsupportedOp(s) => write!(f, "Unsupported op: {s}"),
37            Self::InvalidModel(s) => write!(f, "Invalid model: {s}"),
38            Self::Internal(s) => write!(f, "Internal error: {s}"),
39            Self::Cancelled(s) => write!(f, "Cancelled: {s}"),
40        }
41    }
42}
43
44impl std::error::Error for OnnxError {}
45
46impl From<String> for OnnxError {
47    fn from(s: String) -> Self {
48        Self::Internal(s)
49    }
50}