odbc_api/
error.rs

1use std::io;
2
3use thiserror::Error as ThisError;
4
5use crate::handles::{Diagnostics, Record as DiagnosticRecord, SqlResult, log_diagnostics};
6
7/// Error indicating a failed allocation for a column buffer
8#[derive(Debug)]
9pub struct TooLargeBufferSize {
10    /// Number of elements supposed to be in the buffer.
11    pub num_elements: usize,
12    /// Element size in the buffer in bytes.
13    pub element_size: usize,
14}
15
16impl TooLargeBufferSize {
17    /// Map the column allocation error to an [`crate::Error`] adding the context of which
18    /// column caused the allocation error.
19    pub fn add_context(self, buffer_index: u16) -> Error {
20        Error::TooLargeColumnBufferSize {
21            buffer_index,
22            num_elements: self.num_elements,
23            element_size: self.element_size,
24        }
25    }
26}
27
28#[cfg(feature = "odbc_version_3_5")]
29const ODBC_VERSION_STRING: &str = "3.5";
30#[cfg(not(feature = "odbc_version_3_5"))]
31const ODBC_VERSION_STRING: &str = "3.80";
32
33#[derive(Debug, ThisError)]
34/// Error type used to indicate a low level ODBC call returned with SQL_ERROR.
35pub enum Error {
36    /// Setting connection pooling option failed. Exclusively emitted by
37    /// [`crate::Environment::set_connection_pooling`].
38    #[error("Failed to set connection pooling.")]
39    FailedSettingConnectionPooling,
40    /// Allocating the environment itself fails. Further diagnostics are not available, as they
41    /// would be retrieved using the envirorment handle. Exclusively emitted by
42    /// [`crate::Environment::new`].
43    #[error("Failed to allocate ODBC Environment.")]
44    FailedAllocatingEnvironment,
45    /// This should never happen, given that ODBC driver manager and ODBC driver do not have any
46    /// Bugs. Since we may link vs a bunch of these, better to be on the safe side.
47    #[error(
48        "No Diagnostics available. The ODBC function call to {} returned an error. Sadly neither \
49        the ODBC driver manager, nor the driver were polite enough to leave a diagnostic record \
50        specifying what exactly went wrong.",
51        function
52    )]
53    NoDiagnostics {
54        /// ODBC API call which returned error without producing a diagnostic record.
55        function: &'static str,
56    },
57    /// SQL Error had been returned by a low level ODBC function call. A Diagnostic record is
58    /// obtained and associated with this error.
59    #[error("ODBC emitted an error calling '{function}':\n{record}")]
60    Diagnostics {
61        /// Diagnostic record returned by the ODBC driver manager
62        record: DiagnosticRecord,
63        /// ODBC API call which produced the diagnostic record
64        function: &'static str,
65    },
66    /// A user dialog to complete the connection string has been aborted.
67    #[error("The dialog shown to provide or complete the connection string has been aborted.")]
68    AbortedConnectionStringCompletion,
69    /// An error returned if we fail to set the ODBC version
70    #[error(
71        "The ODBC diver manager installed in your system does not seem to support ODBC API version \
72        {ODBC_VERSION_STRING}. Which is required by this application. Most likely you need to \
73        update your driver manager. Your driver manager is most likely unixODBC if you run on a \
74        Linux. Diagnostic record returned by SQLSetEnvAttr:\n{0}"
75    )]
76    UnsupportedOdbcApiVersion(DiagnosticRecord),
77    /// An error emitted by an `std::io::ReadBuf` implementation used as an input argument.
78    #[error("Sending data to the database at statement execution time failed. IO error:\n{0}")]
79    FailedReadingInput(io::Error),
80    /// Driver returned "invalid attribute" then setting the row array size. Most likely the array
81    /// size is too large. Instead of returing "option value changed (SQLSTATE 01S02)" as suggested
82    /// in <https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlsetstmtattr-function> the
83    /// driver returned an error instead.
84    #[error(
85        "An invalid row array size (aka. batch size) has been set. The ODBC drivers should just \
86        emit a warning and emmit smaller batches, but not all do (yours does not at least). Try \
87        fetching data from the database in smaller batches.\nRow array size (aka. batch size): \
88        {size}\n Diagnostic record returned by SQLSetEnvAttr:\n{record}"
89    )]
90    InvalidRowArraySize {
91        record: DiagnosticRecord,
92        size: usize,
93    },
94    #[error(
95        "Tried to retrieve a value from the database. The value turned out to be `NULL` yet this \
96        turned out to not be representable. So the application is written as if the value could \
97        never be `NULL` in the datasource, yet the in actuallity a `NULL` has been returned. \
98        Diagnostic record returned:\n{0}"
99    )]
100    UnableToRepresentNull(DiagnosticRecord),
101    /// There are plenty of issues in the net about Oracle ODBC driver not supporting 64Bit. This
102    /// message, should make it easier identify what is going on, since the message emmitted by,
103    /// Oracles ODBC driver is a bit cryptic: `[Oracle][ODBC]Invalid SQL data type <-25>`.
104    #[error(
105        "SQLFetch came back with an error indicating you specified an invalid SQL Type. You very \
106        likely did not do that however. Actually SQLFetch is not supposed to return that error \
107        type.  You should have received it back than you were still binding columns or parameters. \
108        All this is circumstancial evidence that you are using an Oracle Database and want to use \
109        64Bit integers, which are not supported by Oracles ODBC driver manager. In case this \
110        diagnose is wrong the original error is:\n{0}."
111    )]
112    OracleOdbcDriverDoesNotSupport64Bit(DiagnosticRecord),
113    #[error(
114        "There is not enough memory to allocate enough memory for a column buffer. Number of \
115        elements requested for the column buffer: {num_elements}; Size needed to hold the largest \
116        possible element: {element_size}."
117    )]
118    TooLargeColumnBufferSize {
119        /// Zero based column buffer index. Note that this is different from the 1 based column
120        /// index.
121        buffer_index: u16,
122        num_elements: usize,
123        /// `usize::MAX` may be used to indicate a missing aupper bound of an element.
124        element_size: usize,
125    },
126    #[error(
127        "A value (at least one) is too large to be written into the allocated buffer without \
128        truncation. Size in bytes indicated by ODBC driver: {indicator:?}"
129    )]
130    TooLargeValueForBuffer {
131        /// Length of the complete value in bytes as reported by the ODBC driver. If the length is
132        /// not known, this is `None`.
133        indicator: Option<usize>,
134        /// Index of the buffer in which the truncation occurred.
135        buffer_index: usize,
136    },
137}
138
139impl Error {
140    /// Allows for mapping the error variant from the "catch all" diagnostic to a more specific one
141    /// offering the oppertunity to provide context in the error message.
142    fn provide_context_for_diagnostic<F>(self, f: F) -> Self
143    where
144        F: FnOnce(DiagnosticRecord, &'static str) -> Error,
145    {
146        if let Error::Diagnostics { record, function } = self {
147            f(record, function)
148        } else {
149            self
150        }
151    }
152}
153
154/// Convinience for easily providing more context to errors without an additional call to `map_err`
155pub(crate) trait ExtendResult {
156    fn provide_context_for_diagnostic<F>(self, f: F) -> Self
157    where
158        F: FnOnce(DiagnosticRecord, &'static str) -> Error;
159}
160
161impl<T> ExtendResult for Result<T, Error> {
162    fn provide_context_for_diagnostic<F>(self, f: F) -> Self
163    where
164        F: FnOnce(DiagnosticRecord, &'static str) -> Error,
165    {
166        self.map_err(|error| error.provide_context_for_diagnostic(f))
167    }
168}
169
170impl SqlResult<()> {
171    /// Use this instead of [`Self::into_result`] if you expect [`SqlResult::NoData`] to be a
172    /// valid value. [`SqlResult::NoData`] is mapped to `Ok(false)`, all other success values are
173    /// `Ok(true)`.
174    pub fn into_result_bool(self, handle: &impl Diagnostics) -> Result<bool, Error> {
175        self.on_success(|| true)
176            .on_no_data(|| false)
177            .into_result(handle)
178    }
179}
180
181// Define that here rather than in `sql_result` mod to keep the `handles` module entirely agnostic
182// about the top level `Error` type.
183impl<T> SqlResult<T> {
184    /// `true` for [`Self::SuccessWithInfo`] and [`Self::Error`]. If `true` one might expect
185    /// diagnostic records to be present. If `false` it would indicate their absense.
186    pub fn has_diganostics(&self) -> bool {
187        matches!(
188            self,
189            SqlResult::SuccessWithInfo(_) | SqlResult::Error { function: _ }
190        )
191    }
192
193    /// [`Self::Success`] and [`Self::SuccessWithInfo`] are mapped to Ok. In case of
194    /// [`Self::SuccessWithInfo`] any diagnostics are logged. [`Self::Error`] is mapped to error.
195    /// Other states [`Self::NoData]` and [`Self::NeedData`] would lead to a panic. Most ODBC
196    /// functions are not suppossed to return these status codes.
197    pub fn into_result(self, handle: &impl Diagnostics) -> Result<T, Error> {
198        if self.has_diganostics() {
199            log_diagnostics(handle);
200        }
201        self.into_result_without_logging(handle)
202    }
203
204    /// [`Self::Success`] and [`Self::SuccessWithInfo`] are mapped to Ok. [`Self::Error`] is mapped
205    /// to error. Other states [`Self::NoData]` and [`Self::NeedData`] would lead to a panic. Most
206    /// ODBC functions are not suppossed to return these status codes.
207    ///
208    /// In case of [`Self::Error`] or [`Self::SuccessWithInfo`] no logging of diagnostic records is
209    /// performed by this method. You may want to use [Self::into_result`] instead.
210    pub fn into_result_without_logging(self, handle: &impl Diagnostics) -> Result<T, Error> {
211        match self {
212            // The function has been executed successfully. Holds result.
213            SqlResult::Success(value) | SqlResult::SuccessWithInfo(value) => Ok(value),
214            SqlResult::Error { function } => {
215                let mut record = DiagnosticRecord::with_capacity(512);
216                if record.fill_from(handle, 1) {
217                    Err(Error::Diagnostics { record, function })
218                } else {
219                    // Anecdotal ways to reach this code paths:
220                    //
221                    // * Inserting a 64Bit integers into an Oracle Database.
222                    // * Specifying invalid drivers (e.g. missing .so the driver itself depends on)
223                    Err(Error::NoDiagnostics { function })
224                }
225            }
226            SqlResult::NoData => {
227                panic!(
228                    "Unexepcted SQL_NO_DATA returned by ODBC function. Use `SqlResult::on_no_data` \
229                    to handle it."
230                )
231            }
232            SqlResult::NeedData => {
233                panic!(
234                    "Unexpected SQL_NEED_DATA returned by ODBC function. Use \
235                    `SqlResult::on_need_data` to handle it."
236                )
237            }
238            SqlResult::StillExecuting => panic!(
239                "SqlResult must not be converted to result while the function is still executing."
240            ),
241        }
242    }
243
244    /// Maps [`SqlResult::Success`] and [`SqlResult::SuccessWithInfo`] to `Some`. Maps
245    /// [`SqlResult::NoData`] to [`SqlResult::Success`] with `None`.
246    pub fn or_no_data(self) -> SqlResult<Option<T>> {
247        self.map(Some).on_no_data(|| None)
248    }
249}