Lines
100 %
Functions
Branches
use super::*;
use axum::body::to_bytes;
use finance::error::TransactionError;
use server::command::CmdError;
use server::db::DBError;
fn mapped(err: CmdError) -> WebError {
WebError::from(err)
}
#[test]
fn args_map_to_bad_request_with_the_bare_message() {
let err = mapped(CmdError::Args("blank tag".to_string()));
assert_eq!(err, WebError::bad_request("blank tag"));
fn finance_maps_to_unprocessable_with_the_innermost_message() {
let err = mapped(CmdError::Finance(TransactionError::ZeroSum.into()));
assert_eq!(err.status, StatusCode::UNPROCESSABLE_ENTITY);
assert_eq!(err.message, "Transaction sum is not zero");
fn finance_storage_failures_are_internal() {
for inner in [
FinanceError::DB(sqlx::Error::Protocol("x".into())),
FinanceError::Internal("secret".to_string()),
] {
let err = mapped(CmdError::Finance(inner));
assert_eq!(
err,
WebError::new(StatusCode::INTERNAL_SERVER_ERROR, "Internal error")
);
fn script_failures_are_internal_without_the_detail() {
let err = mapped(CmdError::Script("JoinError { secret }".to_string()));
fn not_found_maps_to_404_with_its_message() {
let err = mapped(CmdError::NotFound("Transaction not found".to_string()));
assert_eq!(err, WebError::not_found("Transaction not found"));
fn missing_rows_map_to_a_generic_404() {
let causes = [
WebError::from(CmdError::DB(sqlx::Error::RowNotFound)),
WebError::from(CmdError::Server(ServerError::DB(DBError::Sqlx(
sqlx::Error::RowNotFound,
)))),
WebError::from(ServerError::DB(DBError::Sqlx(sqlx::Error::RowNotFound))),
];
for err in causes {
assert_eq!(err, WebError::not_found("Not found"));
fn disabled_maps_to_forbidden() {
let err = mapped(CmdError::Disabled);
assert_eq!(err, WebError::forbidden("This account is disabled"));
fn config_db_and_server_map_to_generic_internal() {
CmdError::Config(server::config::ConfigError::NoConfig("k".into())),
CmdError::DB(sqlx::Error::Protocol("x".into())),
CmdError::Server(ServerError::Lock),
for cause in causes {
let err = mapped(cause);
assert_eq!(err.status, StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(err.message, "Internal error");
fn an_unreachable_database_maps_to_service_unavailable() {
CmdError::DB(sqlx::Error::PoolTimedOut),
CmdError::Server(DBError::PoolBudgetExhausted.into()),
CmdError::Server(DBError::TenantQuarantined.into()),
CmdError::Server(DBError::UnknownTenant.into()),
assert_eq!(err, WebError::unavailable(UNAVAILABLE_MESSAGE));
let server = WebError::from(ServerError::from(DBError::PoolBudgetExhausted));
assert_eq!(server.status, StatusCode::SERVICE_UNAVAILABLE);
fn server_errors_map_like_commands() {
let finance = WebError::from(ServerError::Finance(TransactionError::ZeroSum.into()));
assert_eq!(finance.status, StatusCode::UNPROCESSABLE_ENTITY);
let creation = WebError::from(ServerError::Creation);
assert_eq!(creation, WebError::unprocessable("Can't create an entity"));
let internal = WebError::from(ServerError::Lock);
internal,
fn not_found_carries_404() {
assert_eq!(WebError::not_found("x").status, StatusCode::NOT_FOUND);
#[tokio::test]
async fn into_response_is_json_and_keeps_the_error_in_extensions() {
let response = WebError::unprocessable("bad <b>").into_response();
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
response.extensions().get::<WebError>(),
Some(&WebError::unprocessable("bad <b>"))
let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
body,
serde_json::json!({"status": "error", "message": "bad <b>"})
fn finance_wrapped_in_server_error_is_still_shown() {
let err = mapped(CmdError::Server(ServerError::Finance(
TransactionError::ZeroSum.into(),
)));
fn template_author_errors_are_unprocessable_and_host_failures_internal() {
let author = WebError::from(rpc::TemplateError::Runtime("my-code: boom".to_string()));
assert_eq!(author.status, StatusCode::UNPROCESSABLE_ENTITY);
assert!(author.message.contains("my-code: boom"));
for internal in [
rpc::TemplateError::Host("db down".to_string()),
rpc::TemplateError::Engine("wasm".to_string()),
WebError::from(internal),
async fn a_write_conflict_is_a_retryable_409_with_its_message() -> anyhow::Result<()> {
let conflict = || ServerError::DB(DBError::WriteConflict);
let expected = WebError::new(
StatusCode::CONFLICT,
"The data changed while saving; try again",
assert_eq!(WebError::from(conflict()), expected);
assert_eq!(mapped(CmdError::Server(conflict())), expected);
let response = expected.into_response();
assert_eq!(response.status(), StatusCode::CONFLICT);
let bytes = to_bytes(response.into_body(), usize::MAX).await?;
let body: serde_json::Value = serde_json::from_slice(&bytes)?;
assert_eq!(body["message"], "The data changed while saving; try again");
Ok(())
fn a_missing_price_is_unprocessable_like_the_convert_form() {
let err = mapped(CmdError::NoPrice {
from: uuid::Uuid::nil(),
to: uuid::Uuid::nil(),
});