Skip to main content

calltrace/
error.rs

1//! Error handling for CallTrace library
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum CallTraceError {
7    #[error("Memory allocation failed")]
8    OutOfMemory,
9
10    #[error("Invalid argument: {0}")]
11    InvalidArgument(String),
12
13    #[error("DWARF parsing error: {0}")]
14    DwarfError(String),
15
16    #[error("Register capture failed: {0}")]
17    RegisterError(String),
18
19    #[error("JSON serialization error: {0}")]
20    JsonError(#[from] serde_json::Error),
21
22    #[error("Thread operation failed: {0}")]
23    ThreadError(String),
24
25    #[error("Not supported on this architecture")]
26    NotSupported,
27
28    #[error("Initialization failed: {0}")]
29    InitializationError(String),
30    // TODO(human): Add more error types as needed
31}
32
33pub type Result<T> = std::result::Result<T, CallTraceError>;
34
35/// Convert to C-compatible error code
36impl CallTraceError {
37    pub fn to_c_code(&self) -> i32 {
38        match self {
39            CallTraceError::OutOfMemory => -1,
40            CallTraceError::InvalidArgument(_) => -2,
41            CallTraceError::DwarfError(_) => -3,
42            CallTraceError::RegisterError(_) => -4,
43            CallTraceError::JsonError(_) => -5,
44            CallTraceError::ThreadError(_) => -6,
45            CallTraceError::NotSupported => -7,
46            CallTraceError::InitializationError(_) => -8,
47        }
48    }
49}