refactor: replace failure

- failure is deprecated
- thiserror for deriving Error impl
- anyhow for highlevel error handling
This commit is contained in:
Friedel Ziegelmayer
2020-04-10 22:39:28 +02:00
committed by GitHub
parent d31265895d
commit 24f4cbbb27
38 changed files with 250 additions and 656 deletions

View File

@@ -14,50 +14,28 @@ use crate::dc_tools::*;
use crate::param::*;
use crate::peerstate::*;
#[derive(Debug, Fail)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[fail(display = "Sqlite Error: {:?}", _0)]
Sql(#[cause] rusqlite::Error),
#[fail(display = "Sqlite Connection Pool Error: {:?}", _0)]
ConnectionPool(#[cause] r2d2::Error),
#[fail(display = "Sqlite: Connection closed")]
#[error("Sqlite Error: {0:?}")]
Sql(#[from] rusqlite::Error),
#[error("Sqlite Connection Pool Error: {0:?}")]
ConnectionPool(#[from] r2d2::Error),
#[error("Sqlite: Connection closed")]
SqlNoConnection,
#[fail(display = "Sqlite: Already open")]
#[error("Sqlite: Already open")]
SqlAlreadyOpen,
#[fail(display = "Sqlite: Failed to open")]
#[error("Sqlite: Failed to open")]
SqlFailedToOpen,
#[fail(display = "{:?}", _0)]
Io(#[cause] std::io::Error),
#[fail(display = "{:?}", _0)]
BlobError(#[cause] crate::blob::BlobError),
#[error("{0}")]
Io(#[from] std::io::Error),
#[error("{0:?}")]
BlobError(#[from] crate::blob::BlobError),
#[error("{0}")]
Other(#[from] crate::error::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
impl From<rusqlite::Error> for Error {
fn from(err: rusqlite::Error) -> Error {
Error::Sql(err)
}
}
impl From<r2d2::Error> for Error {
fn from(err: r2d2::Error) -> Error {
Error::ConnectionPool(err)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::Io(err)
}
}
impl From<crate::blob::BlobError> for Error {
fn from(err: crate::blob::BlobError) -> Error {
Error::BlobError(err)
}
}
/// A wrapper around the underlying Sqlite3 object.
#[derive(DebugStub)]
pub struct Sql {
@@ -96,7 +74,7 @@ impl Sql {
pub fn open(&self, context: &Context, dbfile: &std::path::Path, readonly: bool) -> bool {
match open(context, self, dbfile, readonly) {
Ok(_) => true,
Err(crate::error::Error::SqlError(Error::SqlAlreadyOpen)) => false,
Err(Error::SqlAlreadyOpen) => false,
Err(_) => {
self.close(context);
false
@@ -388,14 +366,14 @@ fn open(
sql: &Sql,
dbfile: impl AsRef<std::path::Path>,
readonly: bool,
) -> crate::error::Result<()> {
) -> Result<()> {
if sql.is_open() {
error!(
context,
"Cannot open, database \"{:?}\" already opened.",
dbfile.as_ref(),
);
return Err(Error::SqlAlreadyOpen.into());
return Err(Error::SqlAlreadyOpen);
}
let mut open_flags = OpenFlags::SQLITE_OPEN_NO_MUTEX;
@@ -545,7 +523,7 @@ fn open(
dbfile.as_ref(),
);
// cannot create the tables - maybe we cannot write?
return Err(Error::SqlFailedToOpen.into());
return Err(Error::SqlFailedToOpen);
} else {
sql.set_raw_config_int(context, "dbversion", 0)?;
}