libduckdb_sys/
error.rs

1use crate::duckdb_state;
2use std::{error, fmt};
3
4/// Error Codes
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum ErrorCode {
8    /// Internal logic error in SQLite
9    InternalMalfunction,
10    /// Access permission denied
11    PermissionDenied,
12    /// Callback routine requested an abort
13    OperationAborted,
14    /// The database file is locked
15    DatabaseBusy,
16    /// A table in the database is locked
17    DatabaseLocked,
18    /// A malloc() failed
19    OutOfMemory,
20    /// Attempt to write a readonly database
21    ReadOnly,
22    /// Operation terminated by sqlite3_interrupt()
23    OperationInterrupted,
24    /// Some kind of disk I/O error occurred
25    SystemIoFailure,
26    /// The database disk image is malformed
27    DatabaseCorrupt,
28    /// Unknown opcode in sqlite3_file_control()
29    NotFound,
30    /// Insertion failed because database is full
31    DiskFull,
32    /// Unable to open the database file
33    CannotOpen,
34    /// Database lock protocol error
35    FileLockingProtocolFailed,
36    /// The database schema changed
37    SchemaChanged,
38    /// String or BLOB exceeds size limit
39    TooBig,
40    /// Abort due to constraint violation
41    ConstraintViolation,
42    /// Data type mismatch
43    TypeMismatch,
44    /// Library used incorrectly
45    ApiMisuse,
46    /// Uses OS features not supported on host
47    NoLargeFileSupport,
48    /// Authorization denied
49    AuthorizationForStatementDenied,
50    /// 2nd parameter to sqlite3_bind out of range
51    ParameterOutOfRange,
52    /// File opened that is not a database file
53    NotADatabase,
54    /// SQL error or missing database
55    Unknown,
56}
57
58#[derive(Clone, Copy, Debug, PartialEq, Eq)]
59pub struct Error {
60    pub code: ErrorCode,
61    pub extended_code: duckdb_state,
62}
63
64impl Error {
65    pub fn new(result_code: duckdb_state) -> Self {
66        Self {
67            code: ErrorCode::Unknown,
68            extended_code: result_code,
69        }
70    }
71}
72
73impl fmt::Display for Error {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        write!(
76            f,
77            "Error code {}: {}",
78            self.extended_code,
79            code_to_str(self.extended_code)
80        )
81    }
82}
83
84impl error::Error for Error {
85    fn description(&self) -> &str {
86        code_to_str(self.extended_code)
87    }
88}
89
90pub fn code_to_str(_: duckdb_state) -> &'static str {
91    "Unknown error code"
92}