Skip to main content

oxibonsai_model/
error.rs

1//! Error types for the model crate.
2
3use thiserror::Error;
4
5/// Result type alias for model operations.
6pub type ModelResult<T> = Result<T, ModelError>;
7
8/// Errors that can occur during model construction and forward pass.
9#[derive(Error, Debug)]
10pub enum ModelError {
11    /// A required tensor was not found during model loading.
12    #[error("missing tensor: {name}")]
13    MissingTensor { name: String },
14
15    /// Tensor shape doesn't match expected dimensions.
16    #[error("shape mismatch for '{name}': expected {expected:?}, got {actual:?}")]
17    ShapeMismatch {
18        name: String,
19        expected: Vec<usize>,
20        actual: Vec<usize>,
21    },
22
23    /// Sequence length exceeds model's maximum context length.
24    #[error("sequence length {seq_len} exceeds max context {max_ctx}")]
25    SequenceTooLong { seq_len: usize, max_ctx: usize },
26
27    /// Underlying core error.
28    #[error("core: {0}")]
29    Core(#[from] oxibonsai_core::error::BonsaiError),
30
31    /// Underlying kernel error.
32    #[error("kernel: {0}")]
33    Kernel(#[from] oxibonsai_kernels::error::KernelError),
34
35    /// Internal error (e.g. poisoned mutex).
36    #[error("internal: {0}")]
37    Internal(String),
38}
39
40impl ModelError {
41    /// Return a short, stable error code string for monitoring and alerting.
42    pub fn error_code(&self) -> &str {
43        match self {
44            Self::MissingTensor { .. } => "MISSING_TENSOR",
45            Self::ShapeMismatch { .. } => "SHAPE_MISMATCH",
46            Self::SequenceTooLong { .. } => "SEQUENCE_TOO_LONG",
47            Self::Core(_) => "CORE_ERROR",
48            Self::Kernel(_) => "KERNEL_ERROR",
49            Self::Internal(_) => "INTERNAL_ERROR",
50        }
51    }
52}