foundry_local_sdk/
error.rs1use thiserror::Error;
2
3#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
5#[non_exhaustive]
6pub enum NativeErrorCode {
7 Ok,
8 NotImplemented,
9 Internal,
10 InvalidArgument,
11 InvalidUsage,
12 OperationCancelled,
13 Network,
14 Unknown(i32),
15}
16
17#[derive(Debug, Error)]
19pub enum FoundryLocalError {
20 #[error("native error ({code:?}): {message}")]
22 Native {
23 code: NativeErrorCode,
24 message: String,
25 },
26 #[error("library load error: {reason}")]
28 LibraryLoad { reason: String },
29 #[error("command execution error: {reason}")]
31 CommandExecution { reason: String },
32 #[error("invalid configuration: {reason}")]
34 InvalidConfiguration { reason: String },
35 #[error("model operation error: {reason}")]
37 ModelOperation { reason: String },
38 #[error("HTTP request error: {0}")]
40 HttpRequest(#[from] reqwest::Error),
41 #[error("serialization error: {0}")]
43 Serialization(#[from] serde_json::Error),
44 #[error("validation error: {reason}")]
46 Validation { reason: String },
47 #[error("I/O error: {0}")]
49 Io(#[from] std::io::Error),
50 #[error("internal error: {reason}")]
52 Internal { reason: String },
53}
54
55impl FoundryLocalError {
56 pub fn native_code(&self) -> Option<NativeErrorCode> {
58 match self {
59 Self::Native { code, .. } => Some(*code),
60 _ => None,
61 }
62 }
63
64 pub fn native_message(&self) -> Option<&str> {
66 match self {
67 Self::Native { message, .. } => Some(message),
68 _ => None,
69 }
70 }
71}
72
73pub type Result<T> = std::result::Result<T, FoundryLocalError>;
75
76#[cfg(test)]
77mod tests {
78 use super::{FoundryLocalError, NativeErrorCode};
79
80 #[test]
81 fn native_error_exposes_code_and_message() {
82 let error = FoundryLocalError::Native {
83 code: NativeErrorCode::Network,
84 message: "connection failed".into(),
85 };
86
87 assert_eq!(error.native_code(), Some(NativeErrorCode::Network));
88 assert_eq!(error.native_message(), Some("connection failed"));
89 }
90
91 #[test]
92 fn non_native_error_has_no_native_details() {
93 let error = FoundryLocalError::Validation {
94 reason: "invalid".into(),
95 };
96
97 assert_eq!(error.native_code(), None);
98 assert_eq!(error.native_message(), None);
99 }
100}