-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patherror.rs
More file actions
88 lines (79 loc) · 2.11 KB
/
error.rs
File metadata and controls
88 lines (79 loc) · 2.11 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
use std::cell::RefCell;
use std::ffi::{c_char, CString};
use std::ptr;
#[repr(i32)]
#[derive(Clone, Copy, Debug)]
pub enum ErrorCode {
InvalidArgument = 1,
Utf8 = 2,
Runtime = 3,
DatasetOpen = 4,
DatasetCountRows = 5,
FragmentScan = 6,
StreamCreate = 7,
StreamNext = 8,
SchemaExport = 9,
BatchExport = 10,
KnnSchema = 11,
KnnStreamCreate = 12,
ExplainPlan = 13,
FtsSchema = 14,
FtsStreamCreate = 15,
HybridStreamCreate = 17,
DatasetWriteOpen = 18,
DatasetWriteBatch = 19,
DatasetWriteFinish = 20,
NamespaceListTables = 21,
NamespaceDescribeTable = 22,
DirNamespaceListTables = 23,
DatasetWriteFinishUncommitted = 24,
DatasetCommitTransaction = 25,
DirNamespaceDropTable = 26,
DatasetDelete = 27,
DatasetUpdateOverwrite = 28,
}
struct LastError {
code: i32,
message: CString,
}
thread_local! {
static LAST_ERROR: RefCell<Option<LastError>> = const { RefCell::new(None) };
}
fn sanitize_message(message: &str) -> CString {
match CString::new(message) {
Ok(v) => v,
Err(_) => CString::new(message.replace('\0', "\\0"))
.unwrap_or_else(|_| CString::new("invalid error message").unwrap()),
}
}
pub fn clear_last_error() {
LAST_ERROR.with(|e| {
*e.borrow_mut() = None;
});
}
pub fn set_last_error(code: ErrorCode, message: impl AsRef<str>) {
let code = code as i32;
let message = sanitize_message(message.as_ref());
LAST_ERROR.with(|e| {
*e.borrow_mut() = Some(LastError { code, message });
});
}
#[no_mangle]
pub extern "C" fn lance_last_error_code() -> i32 {
LAST_ERROR.with(|e| e.borrow().as_ref().map(|v| v.code).unwrap_or(0))
}
#[no_mangle]
pub extern "C" fn lance_last_error_message() -> *const c_char {
LAST_ERROR.with(|e| match e.borrow_mut().take() {
Some(err) => err.message.into_raw() as *const c_char,
None => ptr::null(),
})
}
#[no_mangle]
pub unsafe extern "C" fn lance_free_string(s: *const c_char) {
if !s.is_null() {
unsafe {
let _ = CString::from_raw(s as *mut c_char);
}
}
}