Skip to main content

mssql_codec/
error.rs

1//! Codec error types.
2
3use thiserror::Error;
4
5/// Errors that can occur during packet encoding/decoding.
6#[derive(Debug, Error)]
7#[non_exhaustive]
8pub enum CodecError {
9    /// IO error during read/write operations.
10    #[error("IO error: {0}")]
11    Io(#[from] std::io::Error),
12
13    /// Protocol-level error from tds-protocol.
14    #[error("protocol error: {0}")]
15    Protocol(#[from] tds_protocol::ProtocolError),
16
17    /// Packet too large.
18    #[error("packet too large: {size} bytes (max {max})")]
19    PacketTooLarge {
20        /// Actual packet size.
21        size: usize,
22        /// Maximum allowed size.
23        max: usize,
24    },
25
26    /// Incomplete packet data.
27    #[error("incomplete packet: need {needed} more bytes")]
28    IncompletePacket {
29        /// Bytes needed to complete the packet.
30        needed: usize,
31    },
32
33    /// Invalid packet header.
34    #[error("invalid packet header")]
35    InvalidHeader,
36
37    /// Connection closed unexpectedly.
38    #[error("connection closed")]
39    ConnectionClosed,
40
41    /// Encoding error.
42    #[error("encoding error: {0}")]
43    Encoding(String),
44
45    /// Decoding error.
46    #[error("decoding error: {0}")]
47    Decoding(String),
48}
49
50impl CodecError {
51    /// Check if this error is transient and may succeed on retry.
52    ///
53    /// IO errors and connection closures are typically transient.
54    /// Protocol, encoding, and header errors are terminal.
55    #[must_use]
56    pub fn is_transient(&self) -> bool {
57        match self {
58            Self::Io(_) | Self::ConnectionClosed => true,
59            Self::Protocol(e) => e.is_transient(),
60            _ => false,
61        }
62    }
63
64    /// Check if this error is terminal and will never succeed on retry.
65    #[must_use]
66    pub fn is_terminal(&self) -> bool {
67        !self.is_transient()
68    }
69}