Skip to main content

mssql_types/
error.rs

1//! Type conversion error types.
2
3use thiserror::Error;
4
5/// Errors that can occur during type conversion.
6#[derive(Debug, Error)]
7#[non_exhaustive]
8pub enum TypeError {
9    /// Value is null when non-null was expected.
10    #[error("unexpected null value")]
11    UnexpectedNull,
12
13    /// Type mismatch during conversion.
14    #[error("type mismatch: expected {expected}, got {actual}")]
15    TypeMismatch {
16        /// Expected type name.
17        expected: &'static str,
18        /// Actual type name.
19        actual: String,
20    },
21
22    /// Value is out of range for target type.
23    #[error("value out of range for {target_type}")]
24    OutOfRange {
25        /// Target type name.
26        target_type: &'static str,
27    },
28
29    /// Invalid encoding in string data.
30    #[error("invalid string encoding: {0}")]
31    InvalidEncoding(String),
32
33    /// Invalid binary data.
34    #[error("invalid binary data: {0}")]
35    InvalidBinary(String),
36
37    /// Invalid date/time value.
38    #[error("invalid date/time: {0}")]
39    InvalidDateTime(String),
40
41    /// Invalid decimal value.
42    #[error("invalid decimal: {0}")]
43    InvalidDecimal(String),
44
45    /// Invalid UUID value.
46    #[error("invalid UUID: {0}")]
47    InvalidUuid(String),
48
49    /// Truncation occurred during conversion.
50    #[error("value truncated: {0}")]
51    Truncation(String),
52
53    /// Unsupported type conversion.
54    #[error("unsupported conversion from {from} to {to}")]
55    UnsupportedConversion {
56        /// Source type.
57        from: String,
58        /// Target type.
59        to: &'static str,
60    },
61
62    /// Buffer too small for value.
63    #[error("buffer too small: need {needed} bytes, have {available}")]
64    BufferTooSmall {
65        /// Bytes needed.
66        needed: usize,
67        /// Bytes available.
68        available: usize,
69    },
70}
71
72impl TypeError {
73    /// Check if this error is transient and may succeed on retry.
74    ///
75    /// Type conversion errors are always terminal — they indicate
76    /// a mismatch between the SQL type and the Rust type, which
77    /// won't resolve itself on retry.
78    #[must_use]
79    pub fn is_transient(&self) -> bool {
80        false
81    }
82
83    /// Check if this error is terminal and will never succeed on retry.
84    ///
85    /// All type errors are terminal.
86    #[must_use]
87    pub fn is_terminal(&self) -> bool {
88        true
89    }
90}