1use crate::duckdb_state;
2use std::{error, fmt};
3
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum ErrorCode {
8 InternalMalfunction,
10 PermissionDenied,
12 OperationAborted,
14 DatabaseBusy,
16 DatabaseLocked,
18 OutOfMemory,
20 ReadOnly,
22 OperationInterrupted,
24 SystemIoFailure,
26 DatabaseCorrupt,
28 NotFound,
30 DiskFull,
32 CannotOpen,
34 FileLockingProtocolFailed,
36 SchemaChanged,
38 TooBig,
40 ConstraintViolation,
42 TypeMismatch,
44 ApiMisuse,
46 NoLargeFileSupport,
48 AuthorizationForStatementDenied,
50 ParameterOutOfRange,
52 NotADatabase,
54 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}