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 /// The in-flight request was cancelled via an Attention signal and the
42 /// server acknowledged the cancellation (DONE_ATTN). The connection is
43 /// drained and remains usable for new requests.
44 #[error("request cancelled")]
45 Cancelled,
46
47 /// Encoding error.
48 #[error("encoding error: {0}")]
49 Encoding(String),
50
51 /// Decoding error.
52 #[error("decoding error: {0}")]
53 Decoding(String),
54}
55
56impl CodecError {
57 /// Check if this error is transient and may succeed on retry.
58 ///
59 /// IO errors and connection closures are typically transient.
60 /// Protocol, encoding, and header errors are terminal.
61 #[must_use]
62 pub fn is_transient(&self) -> bool {
63 match self {
64 Self::Io(_) | Self::ConnectionClosed => true,
65 Self::Protocol(e) => e.is_transient(),
66 _ => false,
67 }
68 }
69
70 /// Check if this error is terminal and will never succeed on retry.
71 #[must_use]
72 pub fn is_terminal(&self) -> bool {
73 !self.is_transient()
74 }
75}