Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sqlx = { version = "0.8", optional = true, default-features = false, features =
"migrate",
] }
redis = { version = "0.32", optional = true, default-features = false }
validator = { version = "0.20", optional = true }
validator = { version = "0.20", optional = true, features = ["derive"] }
config = { version = "0.15", optional = true }
utoipa = { version = "5.3", optional = true }
tokio = { version = "1", optional = true, features = ["time"] }
Expand Down
21 changes: 21 additions & 0 deletions src/convert/sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,24 @@ impl From<MigrateError> for AppError {
AppError::database(Some(err.to_string()))
}
}

#[cfg(all(test, feature = "sqlx"))]
mod tests {
use std::io;

use super::*;
use crate::AppErrorKind;

#[test]
fn row_not_found_maps_to_not_found() {
let err: AppError = SqlxError::RowNotFound.into();
assert!(matches!(err.kind, AppErrorKind::NotFound));
}

#[test]
fn other_error_maps_to_database() {
let io_err = io::Error::new(io::ErrorKind::Other, "boom");
let err: AppError = SqlxError::Io(io_err).into();
assert!(matches!(err.kind, AppErrorKind::Database));
}
}
18 changes: 18 additions & 0 deletions src/convert/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,21 @@ impl From<Elapsed> for AppError {
AppError::timeout("Operation timed out")
}
}

#[cfg(all(test, feature = "tokio"))]
mod tests {
use tokio::time::{Duration, sleep, timeout};

use super::*;
use crate::AppErrorKind;

#[tokio::test]
async fn elapsed_maps_to_timeout() {
let fut = sleep(Duration::from_millis(20));
let err = timeout(Duration::from_millis(1), fut)
.await
.expect_err("expect timeout");
let app_err: AppError = err.into();
assert!(matches!(app_err.kind, AppErrorKind::Timeout));
}
}
23 changes: 23 additions & 0 deletions src/convert/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,26 @@ impl From<ValidationErrors> for AppError {
AppError::validation(err.to_string())
}
}

#[cfg(all(test, feature = "validator"))]
mod tests {
use validator::Validate;

use super::*;
use crate::AppErrorKind;

#[derive(Validate)]
struct Payload {
#[validate(range(min = 1))]
val: i32
}

#[test]
fn validation_errors_map_to_validation_kind() {
let bad = Payload {
val: 0
};
let err: AppError = bad.validate().unwrap_err().into();
assert!(matches!(err.kind, AppErrorKind::Validation));
}
}
Loading