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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::ffi::CStr;
use std::error;
use std::fmt;
use std::num;
use std::str;
use try_from;
use binding::dpiErrorInfo;
use binding::dpiContext_getError;
use Context;
use to_rust_str;
pub enum Error {
OciError(DbError),
DpiError(DbError),
NullValue,
ParseError(Box<error::Error>),
Overflow(String, &'static str),
InvalidTypeConversion(String, String),
InvalidBindIndex(usize),
InvalidBindName(String),
InvalidColumnIndex(usize),
InvalidColumnName(String),
InvalidAttributeName(String),
UninitializedBindValue,
NoMoreData,
InternalError(String),
}
#[derive(Eq, PartialEq, Clone)]
pub struct ParseOracleTypeError {
typename: &'static str,
}
impl ParseOracleTypeError {
pub fn new(typename: &'static str) -> ParseOracleTypeError {
ParseOracleTypeError {
typename: typename,
}
}
}
impl fmt::Display for ParseOracleTypeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} parse error", self.typename)
}
}
impl fmt::Debug for ParseOracleTypeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ParseOracleTypeError")
}
}
impl error::Error for ParseOracleTypeError {
fn description(&self) -> &str {
"Oracle type parse error"
}
fn cause(&self) -> Option<&error::Error> {
None
}
}
#[derive(Eq, PartialEq, Clone)]
pub struct DbError {
code: i32,
offset: u16,
message: String,
fn_name: String,
action: String,
}
impl DbError {
pub fn new(code: i32, offset: u16, message: String, fn_name: String, action: String) -> DbError {
DbError {
code: code,
offset: offset,
message: message,
fn_name: fn_name,
action: action,
}
}
pub fn code(&self) -> i32 {
self.code
}
pub fn offset(&self) -> u16 {
self.offset
}
pub fn message(&self) -> &String {
&self.message
}
pub fn fn_name(&self) -> &String {
&self.fn_name
}
pub fn action(&self) -> &String {
&self.action
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::OciError(ref err) =>
write!(f, "OCI Error: {}", err.message),
Error::DpiError(ref err) =>
write!(f, "DPI Error: {}", err.message),
Error::NullValue =>
write!(f, "NULL value found"),
Error::ParseError(ref err) =>
write!(f, "{}", err),
Error::Overflow(ref src, dst) =>
write!(f, "number too large to convert {} to {}", src, dst),
Error::InvalidTypeConversion(ref from, ref to) =>
write!(f, "invalid type conversion from {} to {}", from, to),
Error::InvalidBindIndex(ref idx) =>
write!(f, "invalid bind index (one-based): {}", idx),
Error::InvalidBindName(ref name) =>
write!(f, "invalid bind name: {}", name),
Error::InvalidColumnIndex(ref idx) =>
write!(f, "invalid column index (zero-based): {}", idx),
Error::InvalidColumnName(ref name) =>
write!(f, "invalid column name: {}", name),
Error::InvalidAttributeName(ref name) =>
write!(f, "invalid attribute name: {}", name),
Error::UninitializedBindValue =>
write!(f, "Try to access uninitialized bind value"),
Error::NoMoreData =>
write!(f, "No more data to be fetched"),
Error::InternalError(ref msg) =>
write!(f, "Internal Error: {}", msg),
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::OciError(ref err) =>
write!(f, "OCI Error: (code: {}, offset: {}, message:{}, fn_name: {}, action: {})",
err.code, err.offset, err.message, err.fn_name, err.action),
Error::DpiError(ref err) =>
write!(f, "OCI Error: (code: {}, offset: {}, message:{}, fn_name: {}, action: {})",
err.code, err.offset, err.message, err.fn_name, err.action),
Error::NullValue =>
write!(f, "NULLValue"),
Error::ParseError(ref err) =>
write!(f, "ParseError: {:?}", err),
Error::Overflow(ref src, dst) =>
write!(f, "Overflow {{ src: {}, dest: {} }}", src, dst),
Error::InvalidTypeConversion(ref from, ref to) =>
write!(f, "InvalidTypeConversion {{ from: {}, to: {} }}", from, to),
Error::InvalidBindIndex(ref idx) =>
write!(f, "InvalidBindIndex: {}", idx),
Error::InvalidBindName(ref name) =>
write!(f, "InvalidBindName: {}", name),
Error::InvalidColumnIndex(ref idx) =>
write!(f, "InvalidColumnIndex: {}", idx),
Error::InvalidColumnName(ref name) =>
write!(f, "InvalidColumnName: {}", name),
Error::InvalidAttributeName(ref name) =>
write!(f, "InvalidAttributeName: {}", name),
Error::UninitializedBindValue =>
write!(f, "UninitializedBindValue"),
Error::NoMoreData =>
write!(f, "NoMoreData"),
Error::InternalError(_) =>
write!(f, "{}", *self),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::OciError(_) => "Oracle OCI error",
Error::DpiError(_) => "ODPI-C error",
Error::NullValue => "NULL value",
Error::ParseError(_) => "parse error",
Error::Overflow(_, _) => "overflow",
Error::InvalidTypeConversion(_, _) => "invalid type conversion",
Error::InvalidBindIndex(_) => "index bind index",
Error::InvalidBindName(_) => "index bind name",
Error::InvalidColumnIndex(_) => "index column index",
Error::InvalidColumnName(_) => "index column name",
Error::InvalidAttributeName(_) => "index attribute name",
Error::UninitializedBindValue => "uninitialided bind value error",
Error::NoMoreData => "no more data",
Error::InternalError(_) => "internal error",
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::ParseError(ref err) => Some(err.as_ref()),
_ => None,
}
}
}
impl From<ParseOracleTypeError> for Error {
fn from(err: ParseOracleTypeError) -> Self {
Error::ParseError(Box::new(err))
}
}
impl From<num::ParseIntError> for Error {
fn from(err: num::ParseIntError) -> Self {
Error::ParseError(Box::new(err))
}
}
impl From<num::ParseFloatError> for Error {
fn from(err: num::ParseFloatError) -> Self {
Error::ParseError(Box::new(err))
}
}
impl From<try_from::TryFromIntError> for Error {
fn from(err: try_from::TryFromIntError) -> Self {
Error::ParseError(Box::new(err))
}
}
impl From<str::Utf8Error> for Error {
fn from(err: str::Utf8Error) -> Self {
Error::ParseError(Box::new(err))
}
}
pub fn error_from_dpi_error(err: &dpiErrorInfo) -> Error {
let err = DbError::new(err.code, err.offset,
to_rust_str(err.message, err.messageLength),
unsafe { CStr::from_ptr(err.fnName) }.to_string_lossy().into_owned(),
unsafe { CStr::from_ptr(err.action) }.to_string_lossy().into_owned());
if err.message().starts_with("DPI") {
Error::DpiError(err)
} else {
Error::OciError(err)
}
}
pub(crate) fn error_from_context(ctxt: &Context) -> Error {
let mut err: dpiErrorInfo = Default::default();
unsafe {
dpiContext_getError(ctxt.context, &mut err);
};
::error::error_from_dpi_error(&err)
}
macro_rules! chkerr {
($ctxt:expr, $code:expr) => {{
if unsafe { $code } == DPI_SUCCESS as i32 {
()
} else {
return Err(::error::error_from_context($ctxt));
}
}};
($ctxt:expr, $code:expr, $cleanup:stmt) => {{
if unsafe { $code } == DPI_SUCCESS as i32 {
()
} else {
let err = ::error::error_from_context($ctxt);
$cleanup
return Err(err);
}
}};
}