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    /// SQL type rejected as unsupported in this context (e.g. deprecated types).
63    #[error("unsupported SQL type {sql_type}: {reason}")]
64    UnsupportedType {
65        /// The rejected SQL type name.
66        sql_type: String,
67        /// Explanation and suggested alternative.
68        reason: String,
69    },
70
71    /// Buffer too small for value.
72    #[error("buffer too small: need {needed} bytes, have {available}")]
73    BufferTooSmall {
74        /// Bytes needed.
75        needed: usize,
76        /// Bytes available.
77        available: usize,
78    },
79}
80
81impl TypeError {
82    /// Check if this error is transient and may succeed on retry.
83    ///
84    /// Type conversion errors are always terminal — they indicate
85    /// a mismatch between the SQL type and the Rust type, which
86    /// won't resolve itself on retry.
87    #[must_use]
88    pub fn is_transient(&self) -> bool {
89        false
90    }
91
92    /// Check if this error is terminal and will never succeed on retry.
93    ///
94    /// All type errors are terminal.
95    #[must_use]
96    pub fn is_terminal(&self) -> bool {
97        true
98    }
99}