libsqlite3_sys_ic/
error.rs

1use std::error;
2use std::fmt;
3use std::os::raw::c_int;
4
5/// Error Codes
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum ErrorCode {
9    /// Internal logic error in SQLite
10    InternalMalfunction,
11    /// Access permission denied
12    PermissionDenied,
13    /// Callback routine requested an abort
14    OperationAborted,
15    /// The database file is locked
16    DatabaseBusy,
17    /// A table in the database is locked
18    DatabaseLocked,
19    /// A malloc() failed
20    OutOfMemory,
21    /// Attempt to write a readonly database
22    ReadOnly,
23    /// Operation terminated by sqlite3_interrupt()
24    OperationInterrupted,
25    /// Some kind of disk I/O error occurred
26    SystemIoFailure,
27    /// The database disk image is malformed
28    DatabaseCorrupt,
29    /// Unknown opcode in sqlite3_file_control()
30    NotFound,
31    /// Insertion failed because database is full
32    DiskFull,
33    /// Unable to open the database file
34    CannotOpen,
35    /// Database lock protocol error
36    FileLockingProtocolFailed,
37    /// The database schema changed
38    SchemaChanged,
39    /// String or BLOB exceeds size limit
40    TooBig,
41    /// Abort due to constraint violation
42    ConstraintViolation,
43    /// Data type mismatch
44    TypeMismatch,
45    /// Library used incorrectly
46    ApiMisuse,
47    /// Uses OS features not supported on host
48    NoLargeFileSupport,
49    /// Authorization denied
50    AuthorizationForStatementDenied,
51    /// 2nd parameter to sqlite3_bind out of range
52    ParameterOutOfRange,
53    /// File opened that is not a database file
54    NotADatabase,
55    /// SQL error or missing database
56    Unknown,
57}
58
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60pub struct Error {
61    pub code: ErrorCode,
62    pub extended_code: c_int,
63}
64
65impl Error {
66    #[must_use]
67    pub fn new(result_code: c_int) -> Error {
68        let code = match result_code & 0xff {
69            super::SQLITE_INTERNAL => ErrorCode::InternalMalfunction,
70            super::SQLITE_PERM => ErrorCode::PermissionDenied,
71            super::SQLITE_ABORT => ErrorCode::OperationAborted,
72            super::SQLITE_BUSY => ErrorCode::DatabaseBusy,
73            super::SQLITE_LOCKED => ErrorCode::DatabaseLocked,
74            super::SQLITE_NOMEM => ErrorCode::OutOfMemory,
75            super::SQLITE_READONLY => ErrorCode::ReadOnly,
76            super::SQLITE_INTERRUPT => ErrorCode::OperationInterrupted,
77            super::SQLITE_IOERR => ErrorCode::SystemIoFailure,
78            super::SQLITE_CORRUPT => ErrorCode::DatabaseCorrupt,
79            super::SQLITE_NOTFOUND => ErrorCode::NotFound,
80            super::SQLITE_FULL => ErrorCode::DiskFull,
81            super::SQLITE_CANTOPEN => ErrorCode::CannotOpen,
82            super::SQLITE_PROTOCOL => ErrorCode::FileLockingProtocolFailed,
83            super::SQLITE_SCHEMA => ErrorCode::SchemaChanged,
84            super::SQLITE_TOOBIG => ErrorCode::TooBig,
85            super::SQLITE_CONSTRAINT => ErrorCode::ConstraintViolation,
86            super::SQLITE_MISMATCH => ErrorCode::TypeMismatch,
87            super::SQLITE_MISUSE => ErrorCode::ApiMisuse,
88            super::SQLITE_NOLFS => ErrorCode::NoLargeFileSupport,
89            super::SQLITE_AUTH => ErrorCode::AuthorizationForStatementDenied,
90            super::SQLITE_RANGE => ErrorCode::ParameterOutOfRange,
91            super::SQLITE_NOTADB => ErrorCode::NotADatabase,
92            _ => ErrorCode::Unknown,
93        };
94
95        Error {
96            code,
97            extended_code: result_code,
98        }
99    }
100}
101
102impl fmt::Display for Error {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        write!(
105            f,
106            "Error code {}: {}",
107            self.extended_code,
108            code_to_str(self.extended_code)
109        )
110    }
111}
112
113impl error::Error for Error {
114    fn description(&self) -> &str {
115        code_to_str(self.extended_code)
116    }
117}
118
119// Result codes.
120// Note: These are not public because our bindgen bindings export whichever
121// constants are present in the current version of SQLite. We repeat them here
122// so we don't have to worry about which version of SQLite added which
123// constants, and we only use them to implement code_to_str below.
124
125const SQLITE_NOTICE: c_int = 27;
126const SQLITE_WARNING: c_int = 28;
127
128// Extended result codes.
129
130const SQLITE_ERROR_MISSING_COLLSEQ: c_int = super::SQLITE_ERROR | (1 << 8);
131const SQLITE_ERROR_RETRY: c_int = super::SQLITE_ERROR | (2 << 8);
132const SQLITE_ERROR_SNAPSHOT: c_int = super::SQLITE_ERROR | (3 << 8);
133
134const SQLITE_IOERR_SHMOPEN: c_int = super::SQLITE_IOERR | (18 << 8);
135const SQLITE_IOERR_SHMSIZE: c_int = super::SQLITE_IOERR | (19 << 8);
136const SQLITE_IOERR_SHMLOCK: c_int = super::SQLITE_IOERR | (20 << 8);
137const SQLITE_IOERR_SHMMAP: c_int = super::SQLITE_IOERR | (21 << 8);
138const SQLITE_IOERR_SEEK: c_int = super::SQLITE_IOERR | (22 << 8);
139const SQLITE_IOERR_DELETE_NOENT: c_int = super::SQLITE_IOERR | (23 << 8);
140const SQLITE_IOERR_MMAP: c_int = super::SQLITE_IOERR | (24 << 8);
141const SQLITE_IOERR_GETTEMPPATH: c_int = super::SQLITE_IOERR | (25 << 8);
142const SQLITE_IOERR_CONVPATH: c_int = super::SQLITE_IOERR | (26 << 8);
143const SQLITE_IOERR_VNODE: c_int = super::SQLITE_IOERR | (27 << 8);
144const SQLITE_IOERR_AUTH: c_int = super::SQLITE_IOERR | (28 << 8);
145const SQLITE_IOERR_BEGIN_ATOMIC: c_int = super::SQLITE_IOERR | (29 << 8);
146const SQLITE_IOERR_COMMIT_ATOMIC: c_int = super::SQLITE_IOERR | (30 << 8);
147const SQLITE_IOERR_ROLLBACK_ATOMIC: c_int = super::SQLITE_IOERR | (31 << 8);
148const SQLITE_IOERR_DATA: c_int = super::SQLITE_IOERR | (32 << 8);
149
150const SQLITE_LOCKED_SHAREDCACHE: c_int = super::SQLITE_LOCKED | (1 << 8);
151const SQLITE_LOCKED_VTAB: c_int = super::SQLITE_LOCKED | (2 << 8);
152
153const SQLITE_BUSY_RECOVERY: c_int = super::SQLITE_BUSY | (1 << 8);
154const SQLITE_BUSY_SNAPSHOT: c_int = super::SQLITE_BUSY | (2 << 8);
155const SQLITE_BUSY_TIMEOUT: c_int = super::SQLITE_BUSY | (3 << 8);
156
157const SQLITE_CANTOPEN_NOTEMPDIR: c_int = super::SQLITE_CANTOPEN | (1 << 8);
158const SQLITE_CANTOPEN_ISDIR: c_int = super::SQLITE_CANTOPEN | (2 << 8);
159const SQLITE_CANTOPEN_FULLPATH: c_int = super::SQLITE_CANTOPEN | (3 << 8);
160const SQLITE_CANTOPEN_CONVPATH: c_int = super::SQLITE_CANTOPEN | (4 << 8);
161const SQLITE_CANTOPEN_SYMLINK: c_int = super::SQLITE_CANTOPEN | (6 << 8);
162
163const SQLITE_CORRUPT_VTAB: c_int = super::SQLITE_CORRUPT | (1 << 8);
164const SQLITE_CORRUPT_SEQUENCE: c_int = super::SQLITE_CORRUPT | (2 << 8);
165const SQLITE_CORRUPT_INDEX: c_int = super::SQLITE_CORRUPT | (3 << 8);
166
167const SQLITE_READONLY_RECOVERY: c_int = super::SQLITE_READONLY | (1 << 8);
168const SQLITE_READONLY_CANTLOCK: c_int = super::SQLITE_READONLY | (2 << 8);
169const SQLITE_READONLY_ROLLBACK: c_int = super::SQLITE_READONLY | (3 << 8);
170const SQLITE_READONLY_DBMOVED: c_int = super::SQLITE_READONLY | (4 << 8);
171const SQLITE_READONLY_CANTINIT: c_int = super::SQLITE_READONLY | (5 << 8);
172const SQLITE_READONLY_DIRECTORY: c_int = super::SQLITE_READONLY | (6 << 8);
173
174const SQLITE_ABORT_ROLLBACK: c_int = super::SQLITE_ABORT | (2 << 8);
175
176const SQLITE_CONSTRAINT_CHECK: c_int = super::SQLITE_CONSTRAINT | (1 << 8);
177const SQLITE_CONSTRAINT_COMMITHOOK: c_int = super::SQLITE_CONSTRAINT | (2 << 8);
178const SQLITE_CONSTRAINT_FOREIGNKEY: c_int = super::SQLITE_CONSTRAINT | (3 << 8);
179const SQLITE_CONSTRAINT_FUNCTION: c_int = super::SQLITE_CONSTRAINT | (4 << 8);
180const SQLITE_CONSTRAINT_NOTNULL: c_int = super::SQLITE_CONSTRAINT | (5 << 8);
181const SQLITE_CONSTRAINT_PRIMARYKEY: c_int = super::SQLITE_CONSTRAINT | (6 << 8);
182const SQLITE_CONSTRAINT_TRIGGER: c_int = super::SQLITE_CONSTRAINT | (7 << 8);
183const SQLITE_CONSTRAINT_UNIQUE: c_int = super::SQLITE_CONSTRAINT | (8 << 8);
184const SQLITE_CONSTRAINT_VTAB: c_int = super::SQLITE_CONSTRAINT | (9 << 8);
185const SQLITE_CONSTRAINT_ROWID: c_int = super::SQLITE_CONSTRAINT | (10 << 8);
186const SQLITE_CONSTRAINT_PINNED: c_int = super::SQLITE_CONSTRAINT | (11 << 8);
187const SQLITE_CONSTRAINT_DATATYPE: c_int = super::SQLITE_CONSTRAINT | (12 << 8);
188
189const SQLITE_NOTICE_RECOVER_WAL: c_int = SQLITE_NOTICE | (1 << 8);
190const SQLITE_NOTICE_RECOVER_ROLLBACK: c_int = SQLITE_NOTICE | (2 << 8);
191
192const SQLITE_WARNING_AUTOINDEX: c_int = SQLITE_WARNING | (1 << 8);
193
194const SQLITE_AUTH_USER: c_int = super::SQLITE_AUTH | (1 << 8);
195
196#[must_use]
197pub fn code_to_str(code: c_int) -> &'static str {
198    match code {
199        super::SQLITE_OK        => "Successful result",
200        super::SQLITE_ERROR     => "SQL error or missing database",
201        super::SQLITE_INTERNAL  => "Internal logic error in SQLite",
202        super::SQLITE_PERM      => "Access permission denied",
203        super::SQLITE_ABORT     => "Callback routine requested an abort",
204        super::SQLITE_BUSY      => "The database file is locked",
205        super::SQLITE_LOCKED    => "A table in the database is locked",
206        super::SQLITE_NOMEM     => "A malloc() failed",
207        super::SQLITE_READONLY  => "Attempt to write a readonly database",
208        super::SQLITE_INTERRUPT => "Operation terminated by sqlite3_interrupt()",
209        super::SQLITE_IOERR     => "Some kind of disk I/O error occurred",
210        super::SQLITE_CORRUPT   => "The database disk image is malformed",
211        super::SQLITE_NOTFOUND  => "Unknown opcode in sqlite3_file_control()",
212        super::SQLITE_FULL      => "Insertion failed because database is full",
213        super::SQLITE_CANTOPEN  => "Unable to open the database file",
214        super::SQLITE_PROTOCOL  => "Database lock protocol error",
215        super::SQLITE_EMPTY     => "Database is empty",
216        super::SQLITE_SCHEMA    => "The database schema changed",
217        super::SQLITE_TOOBIG    => "String or BLOB exceeds size limit",
218        super::SQLITE_CONSTRAINT=> "Abort due to constraint violation",
219        super::SQLITE_MISMATCH  => "Data type mismatch",
220        super::SQLITE_MISUSE    => "Library used incorrectly",
221        super::SQLITE_NOLFS     => "Uses OS features not supported on host",
222        super::SQLITE_AUTH      => "Authorization denied",
223        super::SQLITE_FORMAT    => "Auxiliary database format error",
224        super::SQLITE_RANGE     => "2nd parameter to sqlite3_bind out of range",
225        super::SQLITE_NOTADB    => "File opened that is not a database file",
226        SQLITE_NOTICE    => "Notifications from sqlite3_log()",
227        SQLITE_WARNING   => "Warnings from sqlite3_log()",
228        super::SQLITE_ROW       => "sqlite3_step() has another row ready",
229        super::SQLITE_DONE      => "sqlite3_step() has finished executing",
230
231        SQLITE_ERROR_MISSING_COLLSEQ   => "SQLITE_ERROR_MISSING_COLLSEQ",
232        SQLITE_ERROR_RETRY   => "SQLITE_ERROR_RETRY",
233        SQLITE_ERROR_SNAPSHOT   => "SQLITE_ERROR_SNAPSHOT",
234
235        super::SQLITE_IOERR_READ              => "Error reading from disk",
236        super::SQLITE_IOERR_SHORT_READ        => "Unable to obtain number of requested bytes (file truncated?)",
237        super::SQLITE_IOERR_WRITE             => "Error writing to disk",
238        super::SQLITE_IOERR_FSYNC             => "Error flushing data to persistent storage (fsync)",
239        super::SQLITE_IOERR_DIR_FSYNC         => "Error calling fsync on a directory",
240        super::SQLITE_IOERR_TRUNCATE          => "Error attempting to truncate file",
241        super::SQLITE_IOERR_FSTAT             => "Error invoking fstat to get file metadata",
242        super::SQLITE_IOERR_UNLOCK            => "I/O error within xUnlock of a VFS object",
243        super::SQLITE_IOERR_RDLOCK            => "I/O error within xLock of a VFS object (trying to obtain a read lock)",
244        super::SQLITE_IOERR_DELETE            => "I/O error within xDelete of a VFS object",
245        super::SQLITE_IOERR_BLOCKED           => "SQLITE_IOERR_BLOCKED", // no longer used
246        super::SQLITE_IOERR_NOMEM             => "Out of memory in I/O layer",
247        super::SQLITE_IOERR_ACCESS            => "I/O error within xAccess of a VFS object",
248        super::SQLITE_IOERR_CHECKRESERVEDLOCK => "I/O error within then xCheckReservedLock method",
249        super::SQLITE_IOERR_LOCK              => "I/O error in the advisory file locking layer",
250        super::SQLITE_IOERR_CLOSE             => "I/O error within the xClose method",
251        super::SQLITE_IOERR_DIR_CLOSE         => "SQLITE_IOERR_DIR_CLOSE", // no longer used
252        SQLITE_IOERR_SHMOPEN           => "I/O error within the xShmMap method (trying to open a new shared-memory segment)",
253        SQLITE_IOERR_SHMSIZE           => "I/O error within the xShmMap method (trying to resize an existing shared-memory segment)",
254        SQLITE_IOERR_SHMLOCK           => "SQLITE_IOERR_SHMLOCK", // no longer used
255        SQLITE_IOERR_SHMMAP            => "I/O error within the xShmMap method (trying to map a shared-memory segment into process address space)",
256        SQLITE_IOERR_SEEK              => "I/O error within the xRead or xWrite (trying to seek within a file)",
257        SQLITE_IOERR_DELETE_NOENT      => "File being deleted does not exist",
258        SQLITE_IOERR_MMAP              => "I/O error while trying to map or unmap part of the database file into process address space",
259        SQLITE_IOERR_GETTEMPPATH       => "VFS is unable to determine a suitable directory for temporary files",
260        SQLITE_IOERR_CONVPATH          => "cygwin_conv_path() system call failed",
261        SQLITE_IOERR_VNODE             => "SQLITE_IOERR_VNODE", // not documented?
262        SQLITE_IOERR_AUTH              => "SQLITE_IOERR_AUTH",
263        SQLITE_IOERR_BEGIN_ATOMIC      => "SQLITE_IOERR_BEGIN_ATOMIC",
264        SQLITE_IOERR_COMMIT_ATOMIC     => "SQLITE_IOERR_COMMIT_ATOMIC",
265        SQLITE_IOERR_ROLLBACK_ATOMIC   => "SQLITE_IOERR_ROLLBACK_ATOMIC",
266        SQLITE_IOERR_DATA   => "SQLITE_IOERR_DATA",
267
268        SQLITE_LOCKED_SHAREDCACHE      => "Locking conflict due to another connection with a shared cache",
269        SQLITE_LOCKED_VTAB             => "SQLITE_LOCKED_VTAB",
270
271        SQLITE_BUSY_RECOVERY           => "Another process is recovering a WAL mode database file",
272        SQLITE_BUSY_SNAPSHOT           => "Cannot promote read transaction to write transaction because of writes by another connection",
273        SQLITE_BUSY_TIMEOUT           => "SQLITE_BUSY_TIMEOUT",
274
275        SQLITE_CANTOPEN_NOTEMPDIR      => "SQLITE_CANTOPEN_NOTEMPDIR", // no longer used
276        SQLITE_CANTOPEN_ISDIR          => "Attempted to open directory as file",
277        SQLITE_CANTOPEN_FULLPATH       => "Unable to convert filename into full pathname",
278        SQLITE_CANTOPEN_CONVPATH       => "cygwin_conv_path() system call failed",
279        SQLITE_CANTOPEN_SYMLINK       => "SQLITE_CANTOPEN_SYMLINK",
280
281        SQLITE_CORRUPT_VTAB            => "Content in the virtual table is corrupt",
282        SQLITE_CORRUPT_SEQUENCE        => "SQLITE_CORRUPT_SEQUENCE",
283        SQLITE_CORRUPT_INDEX        => "SQLITE_CORRUPT_INDEX",
284
285        SQLITE_READONLY_RECOVERY       => "WAL mode database file needs recovery (requires write access)",
286        SQLITE_READONLY_CANTLOCK       => "Shared-memory file associated with WAL mode database is read-only",
287        SQLITE_READONLY_ROLLBACK       => "Database has hot journal that must be rolled back (requires write access)",
288        SQLITE_READONLY_DBMOVED        => "Database cannot be modified because database file has moved",
289        SQLITE_READONLY_CANTINIT       => "SQLITE_READONLY_CANTINIT",
290        SQLITE_READONLY_DIRECTORY      => "SQLITE_READONLY_DIRECTORY",
291
292        SQLITE_ABORT_ROLLBACK          => "Transaction was rolled back",
293
294        SQLITE_CONSTRAINT_CHECK        => "A CHECK constraint failed",
295        SQLITE_CONSTRAINT_COMMITHOOK   => "Commit hook caused rollback",
296        SQLITE_CONSTRAINT_FOREIGNKEY   => "Foreign key constraint failed",
297        SQLITE_CONSTRAINT_FUNCTION     => "Error returned from extension function",
298        SQLITE_CONSTRAINT_NOTNULL      => "A NOT NULL constraint failed",
299        SQLITE_CONSTRAINT_PRIMARYKEY   => "A PRIMARY KEY constraint failed",
300        SQLITE_CONSTRAINT_TRIGGER      => "A RAISE function within a trigger fired",
301        SQLITE_CONSTRAINT_UNIQUE       => "A UNIQUE constraint failed",
302        SQLITE_CONSTRAINT_VTAB         => "An application-defined virtual table error occurred",
303        SQLITE_CONSTRAINT_ROWID        => "A non-unique rowid occurred",
304        SQLITE_CONSTRAINT_PINNED        => "SQLITE_CONSTRAINT_PINNED",
305        SQLITE_CONSTRAINT_DATATYPE        => "SQLITE_CONSTRAINT_DATATYPE",
306
307        SQLITE_NOTICE_RECOVER_WAL      => "A WAL mode database file was recovered",
308        SQLITE_NOTICE_RECOVER_ROLLBACK => "Hot journal was rolled back",
309
310        SQLITE_WARNING_AUTOINDEX       => "Automatic indexing used - database might benefit from additional indexes",
311
312        SQLITE_AUTH_USER               => "SQLITE_AUTH_USER", // not documented?
313
314        _ => "Unknown error code",
315    }
316}