1use mnn_sys::ErrorCode;
2
3#[doc(hidden)]
4pub type Result<T, E = MNNError> = core::result::Result<T, E>;
5
6pub struct MNNError {
8 kind: error_stack::Report<ErrorKind>,
9}
10
11impl core::fmt::Display for MNNError {
12 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
13 write!(f, "{:?}", self.kind)
14 }
15}
16
17impl core::fmt::Debug for MNNError {
18 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19 write!(f, "{:?}", self.kind)
20 }
21}
22
23impl std::error::Error for MNNError {}
24#[derive(thiserror::Error, Debug)]
28pub enum ErrorKind {
29 #[error("Internal error: {0:?}")]
31 InternalError(ErrorCode),
32 #[error("Invalid input: expected {expected}, got {got}")]
34 SizeMismatch {
35 expected: usize,
37 got: usize,
39 },
40 #[error("Failed to copy tensor")]
42 TensorCopyFailed(i32),
43 #[error("IO Error")]
45 IOError,
46 #[error("Interpreter Error")]
48 InterpreterError,
49 #[error("Ascii Error")]
51 AsciiError,
52 #[error("HalideType mismatch: got {got}")]
54 HalideTypeMismatch {
55 got: &'static str,
57 },
58 #[error("Parse Error")]
60 ParseError,
61 #[error("Sync Error")]
63 SyncError,
64 #[error("Tensor Error")]
66 TensorError,
67 #[error("Dynamic Tensor Error: Tensor needs to be resized before using")]
69 DynamicTensorError,
70}
71
72impl MNNError {
73 #[track_caller]
74 #[doc(hidden)]
75 pub fn new(kind: ErrorKind) -> Self {
76 let kind = error_stack::Report::new(kind);
77 Self { kind }
78 }
79
80 #[track_caller]
81 pub(crate) fn from_error_code(code: ErrorCode) -> Self {
82 Self::new(ErrorKind::InternalError(code))
83 }
84
85 #[inline(always)]
87 pub fn into_inner(self) -> error_stack::Report<ErrorKind> {
88 self.kind
89 }
90}
91
92impl From<ErrorKind> for MNNError {
93 #[track_caller]
94 fn from(kind: ErrorKind) -> Self {
95 Self::new(kind)
96 }
97}
98
99macro_rules! ensure {
100 ($cond:expr, $kind:expr) => {
101 if !($cond) {
102 return Err(crate::error::MNNError::new($kind));
103 }
104 };
105
106 ($cond:expr, $kind:expr; $($printable:expr),*) => {
107 if !($cond) {
108 return Err(crate::error::MNNError::new($kind)
109 $(.attach_printable($printable))*
110 )
111 }
112 };
113
114
115 ($cond:expr, $from:expr, $to:expr) => {
116 if (!$cond) {
117 return Err(error_stack::Report::new($from).change_context($to));
118 }
119 };
120 ($cond:expr, $from:expr, $to:expr; $($printable:expr),*) => {
121 if (!$cond) {
122 return Err(error_stack::Report::new($from)
123 .change_context($to)
124 $(.attach_printable($printable))*
125 )
126 }
127 };
128}
129
130macro_rules! error {
131 ($kind:expr) => {
132 crate::error::MNNError::new($kind)
133 };
134 ($kind:expr, $from:expr) => {
135 crate::error::MNNError::from(error_stack::Report::new($from).change_context($kind))
136 };
137}
138
139pub(crate) use ensure;
140pub(crate) use error;
141
142impl From<error_stack::Report<ErrorKind>> for MNNError {
143 #[track_caller]
144 fn from(report: error_stack::Report<ErrorKind>) -> Self {
145 Self { kind: report }
146 }
147}
148
149impl MNNError {
150 pub(crate) fn attach_printable(
151 self,
152 printable: impl core::fmt::Display + core::fmt::Debug + Send + Sync + 'static,
153 ) -> Self {
154 let kind = self.kind.attach_printable(printable);
155 Self { kind }
156 }
157}