feature/insight-jobs #102
+31
-4
@@ -193,19 +193,26 @@ pub fn connect() -> SqliteConnection {
|
|||||||
conn
|
conn
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct DbError {
|
pub struct DbError {
|
||||||
pub kind: DbErrorKind,
|
pub kind: DbErrorKind,
|
||||||
|
pub source: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DbError {
|
impl DbError {
|
||||||
fn new(kind: DbErrorKind) -> Self {
|
fn new(kind: DbErrorKind) -> Self {
|
||||||
DbError { kind }
|
DbError { kind, source: None }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Capture the source error message AND log it. Callers should use
|
||||||
|
/// this from `map_err` closures so the underlying Diesel/SQLite
|
||||||
|
/// error survives the conversion to `DbError`.
|
||||||
fn log(kind: DbErrorKind, source: impl std::fmt::Display) -> Self {
|
fn log(kind: DbErrorKind, source: impl std::fmt::Display) -> Self {
|
||||||
log::error!("DB {:?}: {}", kind, source);
|
let msg = source.to_string();
|
||||||
DbError { kind }
|
log::error!("DB {:?}: {}", kind, msg);
|
||||||
|
DbError {
|
||||||
|
kind,
|
||||||
|
source: Some(msg),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exists() -> Self {
|
fn exists() -> Self {
|
||||||
@@ -213,6 +220,26 @@ impl DbError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for DbError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match &self.source {
|
||||||
|
Some(s) => write!(f, "DbError {{ kind: {:?}, source: {} }}", self.kind, s),
|
||||||
|
None => write!(f, "DbError {{ kind: {:?} }}", self.kind),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for DbError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match &self.source {
|
||||||
|
Some(s) => write!(f, "{:?}: {}", self.kind, s),
|
||||||
|
None => write!(f, "{:?}", self.kind),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for DbError {}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum DbErrorKind {
|
pub enum DbErrorKind {
|
||||||
AlreadyExists,
|
AlreadyExists,
|
||||||
|
|||||||
@@ -144,6 +144,7 @@ impl PreviewDao for TestPreviewDao {
|
|||||||
} else {
|
} else {
|
||||||
Err(DbError {
|
Err(DbError {
|
||||||
kind: DbErrorKind::UpdateError,
|
kind: DbErrorKind::UpdateError,
|
||||||
|
source: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user