embedded_debugger_mcp/
error.rs

1//! Error types for the debugger MCP server
2
3use thiserror::Error;
4
5/// Main error type for the debugger MCP server
6#[derive(Error, Debug)]
7pub enum DebugError {
8    #[error("Probe not found: {0}")]
9    ProbeNotFound(String),
10
11    #[error("Target not supported: {0}")]
12    TargetNotSupported(String),
13
14    #[error("Connection failed: {0}")]
15    ConnectionFailed(String),
16
17    #[error("Invalid session ID: {0}")]
18    InvalidSession(String),
19
20    #[error("Session limit exceeded (max: {0})")]
21    SessionLimitExceeded(usize),
22
23    #[error("Operation timeout")]
24    OperationTimeout,
25
26    #[error("Invalid address: 0x{0:08x}")]
27    InvalidAddress(u64),
28
29    #[error("Memory access failed: {0}")]
30    MemoryAccessFailed(String),
31
32    #[error("Breakpoint limit exceeded")]
33    BreakpointLimitExceeded,
34
35    #[error("RTT not available")]
36    RttNotAvailable,
37
38    #[error("RTT error: {0}")]
39    RttError(String),
40
41    #[error("Flash operation failed: {0}")]
42    FlashOperationFailed(String),
43
44    #[error("Invalid configuration: {0}")]
45    InvalidConfig(String),
46
47    #[error("Probe error: {0}")]
48    ProbeError(String),
49
50    #[error("IO error: {0}")]
51    IoError(#[from] std::io::Error),
52
53    #[error("Serialization error: {0}")]
54    SerializationError(#[from] serde_json::Error),
55
56    #[error("Internal error: {0}")]
57    InternalError(String),
58}
59
60impl From<probe_rs::Error> for DebugError {
61    fn from(error: probe_rs::Error) -> Self {
62        DebugError::ProbeError(error.to_string())
63    }
64}
65
66impl From<anyhow::Error> for DebugError {
67    fn from(error: anyhow::Error) -> Self {
68        DebugError::InternalError(error.to_string())
69    }
70}
71
72/// Result type alias for convenience
73pub type Result<T> = std::result::Result<T, DebugError>;
74
75/// RTT specific errors
76#[derive(Error, Debug)]
77pub enum RttError {
78    #[error("Control block not found")]
79    ControlBlockNotFound,
80
81    #[error("Channel not found: {0}")]
82    ChannelNotFound(usize),
83
84    #[error("Buffer overflow")]
85    BufferOverflow,
86
87    #[error("Attach failed: {0}")]
88    AttachFailed(String),
89
90    #[error("Read timeout")]
91    ReadTimeout,
92}
93
94impl From<RttError> for DebugError {
95    fn from(error: RttError) -> Self {
96        DebugError::RttError(error.to_string())
97    }
98}
99
100/// Target control errors
101#[derive(Error, Debug)]
102pub enum TargetError {
103    #[error("Target not halted")]
104    TargetNotHalted,
105
106    #[error("Target not running")]
107    TargetNotRunning,
108
109    #[error("Reset failed: {0}")]
110    ResetFailed(String),
111
112    #[error("Halt failed: {0}")]
113    HaltFailed(String),
114
115    #[error("Step failed: {0}")]
116    StepFailed(String),
117}
118
119impl From<TargetError> for DebugError {
120    fn from(error: TargetError) -> Self {
121        DebugError::InternalError(error.to_string())
122    }
123}
124
125/// Memory operation errors
126#[derive(Error, Debug)]
127pub enum MemoryError {
128    #[error("Read failed at address 0x{address:08x}: {reason}")]
129    ReadFailed { address: u64, reason: String },
130
131    #[error("Write failed at address 0x{address:08x}: {reason}")]
132    WriteFailed { address: u64, reason: String },
133
134    #[error("Invalid memory range: 0x{start:08x}-0x{end:08x}")]
135    InvalidRange { start: u64, end: u64 },
136
137    #[error("Memory region not accessible: 0x{address:08x}")]
138    NotAccessible { address: u64 },
139}
140
141impl From<MemoryError> for DebugError {
142    fn from(error: MemoryError) -> Self {
143        DebugError::MemoryAccessFailed(error.to_string())
144    }
145}
146
147/// Flash operation errors
148#[derive(Error, Debug)]
149pub enum FlashError {
150    #[error("Flash erase failed: {0}")]
151    EraseFailed(String),
152
153    #[error("Flash program failed: {0}")]
154    ProgramFailed(String),
155
156    #[error("Flash verify failed: {0}")]
157    VerifyFailed(String),
158
159    #[error("Flash not writable")]
160    NotWritable,
161
162    #[error("Invalid flash address: 0x{0:08x}")]
163    InvalidAddress(u64),
164}
165
166impl From<FlashError> for DebugError {
167    fn from(error: FlashError) -> Self {
168        DebugError::FlashOperationFailed(error.to_string())
169    }
170}