Skip to main content

async_snmp/error/
mod.rs

1//! Error types for async-snmp.
2//!
3//! This module provides:
4//!
5//! - [`Error`] - The main error type (8 variants covering all failure modes)
6//! - [`ErrorStatus`] - SNMP protocol errors returned by agents (RFC 3416)
7//! - [`WalkAbortReason`] - Reasons a walk operation was aborted
8//!
9//! # Error Handling
10//!
11//! Errors are boxed for efficiency: `Result<T> = Result<T, Box<Error>>`.
12//!
13//! ```rust
14//! use async_snmp::{Error, Result};
15//!
16//! fn handle_error(result: Result<()>) {
17//!     match result {
18//!         Ok(()) => println!("Success"),
19//!         Err(e) => match &*e {
20//!             Error::Timeout { target, retries, .. } => {
21//!                 println!("{} unreachable after {} retries", target, retries);
22//!             }
23//!             Error::Auth { target } => {
24//!                 println!("Authentication failed for {}", target);
25//!             }
26//!             _ => println!("Error: {}", e),
27//!         }
28//!     }
29//! }
30//! ```
31
32pub(crate) mod internal;
33
34use std::net::SocketAddr;
35use std::time::Duration;
36
37use crate::oid::Oid;
38
39/// Placeholder target address used when no target is known.
40///
41/// This sentinel value (0.0.0.0:0) is used in error contexts where the
42/// target address cannot be determined (e.g., parsing failures before
43/// the source address is known).
44pub(crate) const UNKNOWN_TARGET: SocketAddr =
45    SocketAddr::new(std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0)), 0);
46
47// Pattern for converting detailed internal errors to simplified public errors:
48//
49// tracing::debug!(
50//     target: "async_snmp::ber",  // or ::auth, ::crypto, etc.
51//     { snmp.offset = 42, snmp.decode_error = "ZeroLengthInteger" },
52//     "decode error details here"
53// );
54// return Err(Error::MalformedResponse { target }.boxed());
55
56/// Result type alias using the library's boxed Error type.
57pub type Result<T> = std::result::Result<T, Box<Error>>;
58
59/// Reason a walk operation was aborted.
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
61pub enum WalkAbortReason {
62    /// Agent returned an OID that is not greater than the previous OID.
63    NonIncreasing,
64    /// Agent returned an OID that was already seen (cycle detected).
65    Cycle,
66}
67
68impl std::fmt::Display for WalkAbortReason {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        match self {
71            Self::NonIncreasing => write!(f, "non-increasing OID"),
72            Self::Cycle => write!(f, "cycle detected"),
73        }
74    }
75}
76
77impl std::error::Error for WalkAbortReason {}
78
79/// The main error type for all async-snmp operations.
80///
81/// This enum covers all possible error conditions including network issues,
82/// protocol errors, authentication failures, and configuration problems.
83///
84/// Errors are boxed (via [`Result`]) to keep the size small on the stack.
85///
86/// # Common Patterns
87///
88/// ## Checking Error Type
89///
90/// Use pattern matching to handle specific error conditions:
91///
92/// ```
93/// use async_snmp::{Error, ErrorStatus};
94///
95/// fn is_retriable(error: &Error) -> bool {
96///     matches!(error,
97///         Error::Timeout { .. } |
98///         Error::Network { .. }
99///     )
100/// }
101///
102/// fn is_access_error(error: &Error) -> bool {
103///     matches!(error,
104///         Error::Snmp { status: ErrorStatus::NoAccess | ErrorStatus::AuthorizationError, .. } |
105///         Error::Auth { .. }
106///     )
107/// }
108/// ```
109#[derive(Debug, thiserror::Error)]
110#[non_exhaustive]
111pub enum Error {
112    /// Network failure (connection refused, unreachable, etc.)
113    #[error("network error communicating with {target}: {source}")]
114    Network {
115        target: SocketAddr,
116        #[source]
117        source: std::io::Error,
118    },
119
120    /// Request timed out after retries.
121    #[error("timeout after {elapsed:?} waiting for {target} ({retries} retries)")]
122    Timeout {
123        target: SocketAddr,
124        elapsed: Duration,
125        retries: u32,
126    },
127
128    /// SNMP protocol error from agent.
129    #[error("SNMP error from {target}: {status} at index {index}")]
130    Snmp {
131        target: SocketAddr,
132        status: ErrorStatus,
133        index: u32,
134        oid: Option<Oid>,
135    },
136
137    /// Authentication/authorization failed.
138    #[error("authentication failed for {target}")]
139    Auth { target: SocketAddr },
140
141    /// Malformed response from agent.
142    #[error("malformed response from {target}")]
143    MalformedResponse { target: SocketAddr },
144
145    /// Walk aborted due to agent misbehavior.
146    #[error("walk aborted for {target}: {reason}")]
147    WalkAborted {
148        target: SocketAddr,
149        reason: WalkAbortReason,
150    },
151
152    /// Invalid configuration.
153    #[error("configuration error: {0}")]
154    Config(Box<str>),
155
156    /// Invalid OID format.
157    #[error("invalid OID: {0}")]
158    InvalidOid(Box<str>),
159}
160
161impl Error {
162    /// Box this error (convenience for constructing boxed errors).
163    pub fn boxed(self) -> Box<Self> {
164        Box::new(self)
165    }
166}
167
168/// SNMP protocol error status codes (RFC 3416).
169///
170/// These codes are returned by SNMP agents to indicate the result of an operation.
171/// The error status is included in the [`Error::Snmp`] variant along with an error
172/// index indicating which varbind caused the error.
173///
174/// # Error Categories
175///
176/// ## SNMPv1 Errors (0-5)
177///
178/// - `NoError` - Operation succeeded
179/// - `TooBig` - Response too large for transport
180/// - `NoSuchName` - OID not found (v1 only; v2c+ uses exceptions)
181/// - `BadValue` - Invalid value in SET
182/// - `ReadOnly` - Attempted write to read-only object
183/// - `GenErr` - Unspecified error
184///
185/// ## SNMPv2c/v3 Errors (6-18)
186///
187/// These provide more specific error information for SET operations:
188///
189/// - `NoAccess` - Object not accessible (access control)
190/// - `WrongType` - Value has wrong ASN.1 type
191/// - `WrongLength` - Value has wrong length
192/// - `WrongValue` - Value out of range or invalid
193/// - `NotWritable` - Object does not support SET
194/// - `AuthorizationError` - Access denied by VACM
195///
196/// # Example
197///
198/// ```
199/// use async_snmp::ErrorStatus;
200///
201/// let status = ErrorStatus::from_i32(2);
202/// assert_eq!(status, ErrorStatus::NoSuchName);
203/// assert_eq!(status.as_i32(), 2);
204/// println!("Error: {}", status); // prints "noSuchName"
205/// ```
206#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
207#[non_exhaustive]
208pub enum ErrorStatus {
209    /// Operation completed successfully (status = 0).
210    NoError,
211    /// Response message would be too large for transport (status = 1).
212    TooBig,
213    /// Requested OID not found (status = 2). SNMPv1 only; v2c+ uses exception values.
214    NoSuchName,
215    /// Invalid value provided in SET request (status = 3).
216    BadValue,
217    /// Attempted to SET a read-only object (status = 4).
218    ReadOnly,
219    /// Unspecified error occurred (status = 5).
220    GenErr,
221    /// Object exists but access is denied (status = 6).
222    NoAccess,
223    /// SET value has wrong ASN.1 type (status = 7).
224    WrongType,
225    /// SET value has incorrect length (status = 8).
226    WrongLength,
227    /// SET value uses wrong encoding (status = 9).
228    WrongEncoding,
229    /// SET value is out of range or otherwise invalid (status = 10).
230    WrongValue,
231    /// Object does not support row creation (status = 11).
232    NoCreation,
233    /// Value is inconsistent with other managed objects (status = 12).
234    InconsistentValue,
235    /// Resource required for SET is unavailable (status = 13).
236    ResourceUnavailable,
237    /// SET commit phase failed (status = 14).
238    CommitFailed,
239    /// SET undo phase failed (status = 15).
240    UndoFailed,
241    /// Access denied by VACM (status = 16).
242    AuthorizationError,
243    /// Object does not support modification (status = 17).
244    NotWritable,
245    /// Named object cannot be created (status = 18).
246    InconsistentName,
247    /// Unknown or future error status code.
248    Unknown(i32),
249}
250
251impl ErrorStatus {
252    /// Create from raw status code.
253    pub fn from_i32(value: i32) -> Self {
254        match value {
255            0 => Self::NoError,
256            1 => Self::TooBig,
257            2 => Self::NoSuchName,
258            3 => Self::BadValue,
259            4 => Self::ReadOnly,
260            5 => Self::GenErr,
261            6 => Self::NoAccess,
262            7 => Self::WrongType,
263            8 => Self::WrongLength,
264            9 => Self::WrongEncoding,
265            10 => Self::WrongValue,
266            11 => Self::NoCreation,
267            12 => Self::InconsistentValue,
268            13 => Self::ResourceUnavailable,
269            14 => Self::CommitFailed,
270            15 => Self::UndoFailed,
271            16 => Self::AuthorizationError,
272            17 => Self::NotWritable,
273            18 => Self::InconsistentName,
274            other => {
275                tracing::warn!(target: "async_snmp::error", { snmp.error_status = other }, "unknown SNMP error status");
276                Self::Unknown(other)
277            }
278        }
279    }
280
281    /// Convert to raw status code.
282    pub fn as_i32(&self) -> i32 {
283        match self {
284            Self::NoError => 0,
285            Self::TooBig => 1,
286            Self::NoSuchName => 2,
287            Self::BadValue => 3,
288            Self::ReadOnly => 4,
289            Self::GenErr => 5,
290            Self::NoAccess => 6,
291            Self::WrongType => 7,
292            Self::WrongLength => 8,
293            Self::WrongEncoding => 9,
294            Self::WrongValue => 10,
295            Self::NoCreation => 11,
296            Self::InconsistentValue => 12,
297            Self::ResourceUnavailable => 13,
298            Self::CommitFailed => 14,
299            Self::UndoFailed => 15,
300            Self::AuthorizationError => 16,
301            Self::NotWritable => 17,
302            Self::InconsistentName => 18,
303            Self::Unknown(code) => *code,
304        }
305    }
306
307    /// Map a v2c+ error status to its v1 equivalent per RFC 2576 Section 4.3.
308    ///
309    /// V1-native statuses (0-5) pass through unchanged.
310    pub fn to_v1(&self) -> Self {
311        match self {
312            // V1-native statuses
313            Self::NoError
314            | Self::TooBig
315            | Self::NoSuchName
316            | Self::BadValue
317            | Self::ReadOnly
318            | Self::GenErr => *self,
319
320            // Value errors -> BadValue
321            Self::WrongType
322            | Self::WrongLength
323            | Self::WrongEncoding
324            | Self::WrongValue
325            | Self::InconsistentValue => Self::BadValue,
326
327            // Access/creation errors -> NoSuchName
328            Self::NoAccess
329            | Self::NotWritable
330            | Self::NoCreation
331            | Self::InconsistentName
332            | Self::AuthorizationError => Self::NoSuchName,
333
334            // Resource/commit errors -> GenErr
335            Self::ResourceUnavailable | Self::CommitFailed | Self::UndoFailed => Self::GenErr,
336
337            Self::Unknown(_) => Self::GenErr,
338        }
339    }
340
341    /// Return the canonical SMI name for this status code.
342    ///
343    /// For `Unknown` variants, returns `None`; callers should format the
344    /// numeric code directly in that case.
345    pub fn as_str(&self) -> Option<&'static str> {
346        match self {
347            Self::NoError => Some("noError"),
348            Self::TooBig => Some("tooBig"),
349            Self::NoSuchName => Some("noSuchName"),
350            Self::BadValue => Some("badValue"),
351            Self::ReadOnly => Some("readOnly"),
352            Self::GenErr => Some("genErr"),
353            Self::NoAccess => Some("noAccess"),
354            Self::WrongType => Some("wrongType"),
355            Self::WrongLength => Some("wrongLength"),
356            Self::WrongEncoding => Some("wrongEncoding"),
357            Self::WrongValue => Some("wrongValue"),
358            Self::NoCreation => Some("noCreation"),
359            Self::InconsistentValue => Some("inconsistentValue"),
360            Self::ResourceUnavailable => Some("resourceUnavailable"),
361            Self::CommitFailed => Some("commitFailed"),
362            Self::UndoFailed => Some("undoFailed"),
363            Self::AuthorizationError => Some("authorizationError"),
364            Self::NotWritable => Some("notWritable"),
365            Self::InconsistentName => Some("inconsistentName"),
366            Self::Unknown(_) => None,
367        }
368    }
369}
370
371impl std::fmt::Display for ErrorStatus {
372    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
373        match self.as_str() {
374            Some(name) => f.write_str(name),
375            None => write!(f, "unknown({})", self.as_i32()),
376        }
377    }
378}
379
380#[cfg(test)]
381mod tests {
382    use super::*;
383
384    #[test]
385    fn walk_abort_reason_is_error() {
386        let reason = WalkAbortReason::NonIncreasing;
387        let err: &dyn std::error::Error = &reason;
388        assert_eq!(err.to_string(), "non-increasing OID");
389    }
390
391    #[test]
392    fn error_status_to_v1_mapping() {
393        // RFC 2576 Section 4.3 mappings
394        // V1 statuses (0-5) pass through unchanged
395        assert_eq!(ErrorStatus::NoError.to_v1(), ErrorStatus::NoError);
396        assert_eq!(ErrorStatus::TooBig.to_v1(), ErrorStatus::TooBig);
397        assert_eq!(ErrorStatus::NoSuchName.to_v1(), ErrorStatus::NoSuchName);
398        assert_eq!(ErrorStatus::BadValue.to_v1(), ErrorStatus::BadValue);
399        assert_eq!(ErrorStatus::ReadOnly.to_v1(), ErrorStatus::ReadOnly);
400        assert_eq!(ErrorStatus::GenErr.to_v1(), ErrorStatus::GenErr);
401
402        // WrongValue/WrongType/WrongLength/WrongEncoding/InconsistentValue -> BadValue
403        assert_eq!(ErrorStatus::WrongValue.to_v1(), ErrorStatus::BadValue);
404        assert_eq!(ErrorStatus::WrongType.to_v1(), ErrorStatus::BadValue);
405        assert_eq!(ErrorStatus::WrongLength.to_v1(), ErrorStatus::BadValue);
406        assert_eq!(ErrorStatus::WrongEncoding.to_v1(), ErrorStatus::BadValue);
407        assert_eq!(
408            ErrorStatus::InconsistentValue.to_v1(),
409            ErrorStatus::BadValue
410        );
411
412        // NoAccess/NotWritable/NoCreation/InconsistentName/AuthorizationError -> NoSuchName
413        assert_eq!(ErrorStatus::NoAccess.to_v1(), ErrorStatus::NoSuchName);
414        assert_eq!(ErrorStatus::NotWritable.to_v1(), ErrorStatus::NoSuchName);
415        assert_eq!(ErrorStatus::NoCreation.to_v1(), ErrorStatus::NoSuchName);
416        assert_eq!(
417            ErrorStatus::InconsistentName.to_v1(),
418            ErrorStatus::NoSuchName
419        );
420        assert_eq!(
421            ErrorStatus::AuthorizationError.to_v1(),
422            ErrorStatus::NoSuchName
423        );
424
425        // ResourceUnavailable/CommitFailed/UndoFailed -> GenErr
426        assert_eq!(
427            ErrorStatus::ResourceUnavailable.to_v1(),
428            ErrorStatus::GenErr
429        );
430        assert_eq!(ErrorStatus::CommitFailed.to_v1(), ErrorStatus::GenErr);
431        assert_eq!(ErrorStatus::UndoFailed.to_v1(), ErrorStatus::GenErr);
432    }
433
434    #[test]
435    fn error_size_budget() {
436        // Error size should stay bounded to avoid bloating Result types.
437        // The largest variant is Error::Snmp which contains Option<Oid>.
438        assert!(
439            std::mem::size_of::<Error>() <= 128,
440            "Error size {} exceeds 128-byte budget",
441            std::mem::size_of::<Error>()
442        );
443
444        // Result<(), Box<Error>> should be pointer-sized (8 bytes on 64-bit).
445        assert_eq!(
446            std::mem::size_of::<Result<()>>(),
447            std::mem::size_of::<*const ()>(),
448            "Result<()> should be pointer-sized"
449        );
450    }
451}