mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-03-15 06:20:50 +00:00
Refactor Error handling
This commit is contained in:
parent
1d16210de7
commit
72ff84e686
22
src/error.rs
22
src/error.rs
@ -1,8 +1,30 @@
|
||||
use ResultCode;
|
||||
|
||||
use std::convert::{From, Into};
|
||||
|
||||
/// An error.
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
pub code: ResultCode,
|
||||
pub message: Option<String>,
|
||||
}
|
||||
|
||||
impl<T> From<T> for Error where T: Into<String> {
|
||||
#[inline]
|
||||
fn from(message: T) -> Error {
|
||||
Error {
|
||||
code: ResultCode::Error,
|
||||
message: Some(message.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ResultCode> for Error {
|
||||
#[inline]
|
||||
fn from(code: ResultCode) -> Error {
|
||||
Error {
|
||||
code: code,
|
||||
message: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
13
src/lib.rs
13
src/lib.rs
@ -3,22 +3,15 @@
|
||||
extern crate libc;
|
||||
extern crate sqlite3_sys as raw;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
macro_rules! raise(
|
||||
($message:expr) => (
|
||||
return Err(::Error { code: ::ResultCode::Error, message: Some($message.to_string()) })
|
||||
);
|
||||
($code:expr, $message:expr) => (
|
||||
return Err(::Error { code: $code, message: $message })
|
||||
);
|
||||
($message:expr) => (return Err(::Error::from($message)));
|
||||
);
|
||||
|
||||
macro_rules! success(
|
||||
($result:expr) => (
|
||||
match $result {
|
||||
::raw::SQLITE_OK => {},
|
||||
code => raise!(::result::code_from_raw(code), None),
|
||||
code => return Err(::Error::from(::result::code_from_raw(code))),
|
||||
}
|
||||
);
|
||||
);
|
||||
@ -63,6 +56,6 @@ pub use statement::{Statement, Binding, Value};
|
||||
|
||||
/// Open a database.
|
||||
#[inline]
|
||||
pub fn open(path: &Path) -> Result<Database> {
|
||||
pub fn open(path: &std::path::Path) -> Result<Database> {
|
||||
Database::open(path)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user