-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy patherror.rs
More file actions
288 lines (265 loc) · 9.56 KB
/
error.rs
File metadata and controls
288 lines (265 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Error, status and result types.
use std::os::raw::c_char;
use std::{ffi::NulError, fmt::Display};
use arrow_schema::ArrowError;
use crate::constants;
pub type AdbcStatusCode = u8;
/// Status of an operation.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Status {
/// No error.
Ok,
/// An unknown error occurred.
Unknown,
/// The operation is not implemented or supported.
NotImplemented,
/// A requested resource was not found.
NotFound,
/// A requested resource already exists.
AlreadyExists,
/// The arguments are invalid, likely a programming error.
/// For instance, they may be of the wrong format, or out of range.
InvalidArguments,
/// The preconditions for the operation are not met, likely a programming error.
/// For instance, the object may be uninitialized, or may have not
/// been fully configured.
InvalidState,
/// Invalid data was processed (not a programming error).
/// For instance, a division by zero may have occurred during query
/// execution.
InvalidData,
/// The database's integrity was affected.
/// For instance, a foreign key check may have failed, or a uniqueness
/// constraint may have been violated.
Integrity,
/// An error internal to the driver or database occurred.
Internal,
/// An I/O error occurred.
/// For instance, a remote service may be unavailable.
IO,
/// The operation was cancelled, not due to a timeout.
Cancelled,
/// The operation was cancelled due to a timeout.
Timeout,
/// Authentication failed.
Unauthenticated,
/// The client is not authorized to perform the given operation.
Unauthorized,
}
/// An ADBC error.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Error {
/// The error message.
pub message: String,
/// The status of the operation.
pub status: Status,
/// A vendor-specific error code, if applicable.
pub vendor_code: i32,
/// A SQLSTATE error code, if provided, as defined by the SQL:2003 standard.
/// If not set, it should be set to `\0\0\0\0\0` or "00000".
pub sqlstate: [c_char; 5], // TODO(alexandreyc): should we move to something else than c_char? (see https://github.com/apache/arrow-adbc/pull/1725#discussion_r1567531539)
/// Additional metadata. Introduced in ADBC 1.1.0.
pub details: Option<Vec<(String, Vec<u8>)>>,
}
/// Result type wrapping [Error].
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
pub fn with_message_and_status(message: impl Into<String>, status: Status) -> Self {
Self {
message: message.into(),
status,
vendor_code: 0,
sqlstate: [0; 5],
details: None,
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let safe_ascii = |c: c_char| -> char {
if c == 0 {
'0'
} else if c >= 32 && c <= 126 {
char::from(c as u8)
} else {
'\u{FFFD}'
}
};
write!(
f,
"{:?}: {} (sqlstate: {}{}{}{}{}, vendor_code: {})",
self.status,
self.message,
safe_ascii(self.sqlstate[0]),
safe_ascii(self.sqlstate[1]),
safe_ascii(self.sqlstate[2]),
safe_ascii(self.sqlstate[3]),
safe_ascii(self.sqlstate[4]),
self.vendor_code
)
}
}
impl std::error::Error for Error {}
impl From<ArrowError> for Error {
fn from(value: ArrowError) -> Self {
Self {
message: value.to_string(),
status: Status::Internal,
vendor_code: 0,
sqlstate: [0; 5],
details: None,
}
}
}
impl From<NulError> for Error {
fn from(value: NulError) -> Self {
Self {
message: format!(
"Interior null byte was found at position {}",
value.nul_position()
),
status: Status::InvalidData,
vendor_code: 0,
sqlstate: [0; 5],
details: None,
}
}
}
impl From<std::str::Utf8Error> for Error {
fn from(value: std::str::Utf8Error) -> Self {
Self {
message: format!("Error while decoding UTF-8: {value}"),
status: Status::Internal,
vendor_code: 0,
sqlstate: [0; 5],
details: None,
}
}
}
impl From<std::ffi::IntoStringError> for Error {
fn from(value: std::ffi::IntoStringError) -> Self {
let error = value.utf8_error();
error.into()
}
}
impl TryFrom<AdbcStatusCode> for Status {
type Error = Error;
fn try_from(value: AdbcStatusCode) -> Result<Self> {
match value {
constants::ADBC_STATUS_OK => Ok(Status::Ok),
constants::ADBC_STATUS_UNKNOWN => Ok(Status::Unknown),
constants::ADBC_STATUS_NOT_IMPLEMENTED => Ok(Status::NotImplemented),
constants::ADBC_STATUS_NOT_FOUND => Ok(Status::NotFound),
constants::ADBC_STATUS_ALREADY_EXISTS => Ok(Status::AlreadyExists),
constants::ADBC_STATUS_INVALID_ARGUMENT => Ok(Status::InvalidArguments),
constants::ADBC_STATUS_INVALID_STATE => Ok(Status::InvalidState),
constants::ADBC_STATUS_INVALID_DATA => Ok(Status::InvalidData),
constants::ADBC_STATUS_INTEGRITY => Ok(Status::Integrity),
constants::ADBC_STATUS_INTERNAL => Ok(Status::Internal),
constants::ADBC_STATUS_IO => Ok(Status::IO),
constants::ADBC_STATUS_CANCELLED => Ok(Status::Cancelled),
constants::ADBC_STATUS_TIMEOUT => Ok(Status::Timeout),
constants::ADBC_STATUS_UNAUTHENTICATED => Ok(Status::Unauthenticated),
constants::ADBC_STATUS_UNAUTHORIZED => Ok(Status::Unauthorized),
v => Err(Error::with_message_and_status(
format!("Unknown status code: {v}"),
Status::InvalidData,
)),
}
}
}
impl From<Status> for AdbcStatusCode {
fn from(value: Status) -> Self {
match value {
Status::Ok => constants::ADBC_STATUS_OK,
Status::Unknown => constants::ADBC_STATUS_UNKNOWN,
Status::NotImplemented => constants::ADBC_STATUS_NOT_IMPLEMENTED,
Status::NotFound => constants::ADBC_STATUS_NOT_FOUND,
Status::AlreadyExists => constants::ADBC_STATUS_ALREADY_EXISTS,
Status::InvalidArguments => constants::ADBC_STATUS_INVALID_ARGUMENT,
Status::InvalidState => constants::ADBC_STATUS_INVALID_STATE,
Status::InvalidData => constants::ADBC_STATUS_INVALID_DATA,
Status::Integrity => constants::ADBC_STATUS_INTEGRITY,
Status::Internal => constants::ADBC_STATUS_INTERNAL,
Status::IO => constants::ADBC_STATUS_IO,
Status::Cancelled => constants::ADBC_STATUS_CANCELLED,
Status::Timeout => constants::ADBC_STATUS_TIMEOUT,
Status::Unauthenticated => constants::ADBC_STATUS_UNAUTHENTICATED,
Status::Unauthorized => constants::ADBC_STATUS_UNAUTHORIZED,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_unset_sqlstate() {
let err = Error::with_message_and_status("something failed", Status::Unknown);
let msg = err.to_string();
assert_eq!(
msg,
"Unknown: something failed (sqlstate: 00000, vendor_code: 0)"
);
}
#[test]
fn display_ascii_sqlstate() {
let err = Error {
message: "constraint violation".into(),
status: Status::Integrity,
vendor_code: 42,
sqlstate: [b'2' as c_char, b'3' as c_char, b'5' as c_char, b'0' as c_char, b'5' as c_char],
details: None,
};
let msg = err.to_string();
assert_eq!(
msg,
"Integrity: constraint violation (sqlstate: 23505, vendor_code: 42)"
);
}
#[test]
fn display_non_printable_sqlstate() {
let err = Error {
message: "bad state".into(),
status: Status::Internal,
vendor_code: 0,
sqlstate: [1, 2, 3, 4, 5],
details: None,
};
let msg = err.to_string();
assert_eq!(
msg,
"Internal: bad state (sqlstate: �����, vendor_code: 0)"
);
}
#[test]
fn display_mixed_sqlstate() {
let err = Error {
message: "mixed".into(),
status: Status::InvalidData,
vendor_code: 7,
sqlstate: [b'H' as c_char, b'V' as c_char, 0, 0, 1],
details: None,
};
let msg = err.to_string();
assert_eq!(
msg,
"InvalidData: mixed (sqlstate: HV00�, vendor_code: 7)"
);
}
}