Skip to main content

gigastt_core/runtime/
error.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5use super::tensor::{ElementType, Shape};
6
7/// Errors produced by the runtime abstraction layer.
8#[derive(Debug, Error)]
9pub enum RuntimeError {
10    #[error("failed to load model: {message}")]
11    LoadFailed { path: PathBuf, message: String },
12
13    #[error("inference failed: {0}")]
14    InferenceFailed(String),
15
16    #[error("invalid tensor shape: expected {expected:?}, got {got:?}")]
17    InvalidShape { expected: Shape, got: Shape },
18
19    #[error("unsupported element type: {0:?}")]
20    UnsupportedElementType(ElementType),
21
22    #[error("invalid input count: expected {expected}, got {got}")]
23    InvalidInputCount { expected: usize, got: usize },
24
25    #[error("tensor data length mismatch: expected {expected}, got {got}")]
26    DataLengthMismatch { expected: usize, got: usize },
27}
28
29#[cfg(test)]
30mod tests {
31    use std::path::PathBuf;
32
33    use super::*;
34
35    #[test]
36    fn test_load_failed_display() {
37        let e = RuntimeError::LoadFailed {
38            path: PathBuf::from("encoder.onnx"),
39            message: "not found".into(),
40        };
41        assert!(
42            !e.to_string().contains("encoder.onnx"),
43            "display must not leak the model path"
44        );
45        assert_eq!(e.to_string(), "failed to load model: not found");
46    }
47
48    #[test]
49    fn test_invalid_shape_display() {
50        let expected = Shape::new(vec![2, 3]);
51        let got = Shape::new(vec![3, 2]);
52        let e = RuntimeError::InvalidShape {
53            expected: expected.clone(),
54            got: got.clone(),
55        };
56        assert!(e.to_string().contains("invalid tensor shape"));
57        assert!(e.to_string().contains("[2, 3]"));
58        assert!(e.to_string().contains("[3, 2]"));
59    }
60
61    #[test]
62    fn test_inference_failed_display() {
63        let e = RuntimeError::InferenceFailed("session is closed".into());
64        assert_eq!(e.to_string(), "inference failed: session is closed");
65    }
66
67    #[test]
68    fn test_invalid_input_count_display() {
69        let e = RuntimeError::InvalidInputCount {
70            expected: 3,
71            got: 2,
72        };
73        assert_eq!(e.to_string(), "invalid input count: expected 3, got 2");
74    }
75
76    #[test]
77    fn test_unsupported_element_type_display() {
78        let e = RuntimeError::UnsupportedElementType(ElementType::I64);
79        assert_eq!(e.to_string(), "unsupported element type: I64");
80    }
81}