Skip to content

Commit

Permalink
[db] Add to_string() to errors #651 (#652)
Browse files Browse the repository at this point in the history
* Update query_error.rs

* Update db_error.rs

* Update query_error.rs

* Update db_error.rs
  • Loading branch information
michaelvlach authored Aug 6, 2023
1 parent 26a4c02 commit 7eb109c
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/agdb/db/db_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ impl DbError {
impl Display for DbError {
fn fmt(&self, f: &mut Formatter<'_>) -> FMTResult {
let location = self.source_location.to_string().replace('\\', "/");
write!(f, "{} (at {})", self.description, location)
if let Some(cause) = &self.cause {
write!(
f,
"{} (at {})\ncaused by\n {}",
self.description, location, cause
)
} else {
write!(f, "{} (at {})", self.description, location)
}
}
}

Expand Down Expand Up @@ -111,23 +119,49 @@ mod tests {
let file = file!();
let col__ = column!();
let line = line!();
let error = DbError::from("file not found");
let error = DbError::from("outer error");
assert_eq!(
error.to_string(),
format!(
"file not found (at {}:{}:{})",
"outer error (at {}:{}:{})",
file.replace('\\', "/"),
line + 1,
col__
)
);
}

#[test]
fn derived_from_display_cause() {
let file = file!();
let column___ = column!();
let line = line!();
let mut error = DbError::from("outer error");
let inner_column_adjusted = column!();
let inner_line = line!();
error.cause = Some(Box::new(DbError::from("inner error")));

assert_eq!(
error.to_string(),
format!(
"outer error (at {}:{}:{})\ncaused by\n inner error (at {}:{}:{})",
file.replace('\\', "/"),
line + 1,
column___,
file.replace('\\', "/"),
inner_line + 1,
inner_column_adjusted,
)
);
}

#[test]
fn derived_from_partial_eq() {
let left = DbError::from(IOError::from(ErrorKind::NotFound));
let right = DbError::from(IOError::from(ErrorKind::NotFound));
assert_eq!(left, right);
}

#[test]
fn derived_from_error() {
let file = file!();
Expand Down
72 changes: 72 additions & 0 deletions src/agdb/query/query_error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::db::db_error::DbError;
use std::error::Error;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FMTResult;
use std::sync::PoisonError;

/// Universal `query` error returned from all query operations.
Expand All @@ -21,6 +25,26 @@ impl From<DbError> for QueryError {
}
}

impl Display for QueryError {
fn fmt(&self, f: &mut Formatter<'_>) -> FMTResult {
if let Some(cause) = &self.cause {
write!(f, "{}\ncaused by\n {}", self.description, cause)
} else {
write!(f, "{}", self.description)
}
}
}

impl Error for QueryError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
if let Some(cause) = &self.cause {
return Some(cause);
}

None
}
}

impl<T> From<PoisonError<T>> for QueryError {
fn from(value: PoisonError<T>) -> Self {
Self {
Expand Down Expand Up @@ -49,6 +73,54 @@ impl From<&str> for QueryError {
mod tests {
use super::*;

#[test]
fn derived_from_display() {
let error = QueryError::from("outer error");
assert_eq!(error.to_string(), format!("outer error"));
}

#[test]
fn derived_from_display_cause() {
let mut error = QueryError::from("outer error");
let file = file!();
let inner_column = column!();
let inner_line = line!();
error.cause = Some(DbError::from("inner error"));

assert_eq!(
error.to_string(),
format!(
"outer error\ncaused by\n inner error (at {}:{}:{})",
file.replace('\\', "/"),
inner_line + 1,
inner_column,
)
);
}

#[test]
fn derived_from_error() {
let mut error = QueryError::from("outer error");
let file = file!();
let col_adjust_ = column!();
let line = line!();
let inner_error = DbError::from("inner error");

assert!(error.source().is_none());

error.cause = Some(inner_error);

assert_eq!(
error.source().unwrap().to_string(),
format!(
"inner error (at {}:{}:{})",
file.replace('\\', "/"),
line + 1,
col_adjust_
)
);
}

#[test]
fn derived_from_debug_and_default() {
format!("{:?}", QueryError::default());
Expand Down

0 comments on commit 7eb109c

Please sign in to comment.