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
use std;
use std::ffi::CStr;
use std::fmt::{self, Display};
use failure::{Backtrace, Context, Fail};
use foundationdb_sys as fdb_sys;
use options;
use tuple;
pub(crate) fn eval(error_code: fdb_sys::fdb_error_t) -> Result<()> {
let rust_code = error_code as i32;
if rust_code == 0 {
Ok(())
} else {
Err(Error::from(error_code))
}
}
#[derive(Debug)]
pub struct Error {
kind: Context<ErrorKind>,
should_retry: bool,
}
#[derive(Debug, Fail)]
pub enum ErrorKind {
#[fail(display = "FoundationDB error({}): {}", error_code, error_str)]
Fdb {
error_code: i32,
error_str: &'static str,
},
#[fail(display = "Internal error with tuple encoding/decoding: {}", error)]
Tuple {
error: tuple::Error,
},
}
pub type Result<T> = std::result::Result<T, Error>;
impl Fail for Error {
fn cause(&self) -> Option<&Fail> {
self.kind.cause()
}
fn backtrace(&self) -> Option<&Backtrace> {
self.kind.backtrace()
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.kind, f)
}
}
impl From<tuple::Error> for Error {
fn from(error: tuple::Error) -> Self {
Error {
kind: Context::new(ErrorKind::Tuple { error }),
should_retry: false,
}
}
}
impl Error {
pub fn from(error_code: fdb_sys::fdb_error_t) -> Self {
let error_str = unsafe { CStr::from_ptr(fdb_sys::fdb_get_error(error_code)) };
Error {
kind: Context::new(ErrorKind::Fdb {
error_code: error_code as i32,
error_str: error_str
.to_str()
.expect("bad error string from FoundationDB"),
}),
should_retry: false,
}
}
pub fn is_maybe_committed(&self) -> bool {
match *self.kind.get_context() {
ErrorKind::Fdb { error_code, .. } => {
let check = unsafe {
fdb_sys::fdb_error_predicate(
options::ErrorPredicate::MaybeCommitted.code() as i32,
error_code,
) as i32
};
check != 0
}
_ => false,
}
}
pub fn is_retryable(&self) -> bool {
match *self.kind.get_context() {
ErrorKind::Fdb { error_code, .. } => {
let check = unsafe {
fdb_sys::fdb_error_predicate(
options::ErrorPredicate::Retryable.code() as i32,
error_code,
) as i32
};
check != 0
}
_ => false,
}
}
pub fn is_retryable_not_committed(&self) -> bool {
match *self.kind.get_context() {
ErrorKind::Fdb { error_code, .. } => {
let check = unsafe {
fdb_sys::fdb_error_predicate(
options::ErrorPredicate::RetryableNotCommitted.code() as i32,
error_code,
) as i32
};
check != 0
}
_ => false,
}
}
pub fn code(&self) -> i32 {
match *self.kind.get_context() {
ErrorKind::Fdb { error_code, .. } => error_code,
_ => 4000,
}
}
pub(crate) fn set_should_retry(&mut self, should_retry: bool) {
self.should_retry = should_retry;
}
pub(crate) fn should_retry(&self) -> bool {
self.should_retry
}
}