Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

fix(bounces): ensure db errors don't get converted to 429 responses #59

Merged
merged 1 commit into from Jun 8, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/bounces/mod.rs
Expand Up @@ -63,10 +63,14 @@ impl From<DbError> for BounceError {
}

impl From<BounceError> for Failure {
fn from(_error: BounceError) -> Failure {
fn from(error: BounceError) -> Failure {
// Eventually we should be able to do something richer than this,
// as per https://github.com/SergioBenitez/Rocket/issues/586.
Failure(Status::TooManyRequests)
Failure(
error
.bounce
.map_or(Status::InternalServerError, |_| Status::TooManyRequests),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

)
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/bounces/test.rs
Expand Up @@ -2,6 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.

use rocket::http::Status;
use serde_json::{self, Value as Json};

use super::*;
Expand Down Expand Up @@ -97,11 +98,13 @@ fn check_soft_bounce() {
"email address violated soft bounce limit"
);
assert_eq!(error.address, "foo@example.com");
if let Some(bounce) = error.bounce {
if let Some(ref bounce) = error.bounce {
assert_eq!(bounce.bounce_type, BounceType::Soft);
} else {
assert!(false, "Error::bounce should be set");
}
let failure: Failure = From::from(error);
assert_eq!(failure.0, Status::TooManyRequests);
}
}
}
Expand Down Expand Up @@ -140,11 +143,13 @@ fn check_hard_bounce() {
"email address violated hard bounce limit"
);
assert_eq!(error.address, "bar@example.com");
if let Some(bounce) = error.bounce {
if let Some(ref bounce) = error.bounce {
assert_eq!(bounce.bounce_type, BounceType::Hard);
} else {
assert!(false, "Error::bounce should be set");
}
let failure: Failure = From::from(error);
assert_eq!(failure.0, Status::TooManyRequests);
}
}
}
Expand Down Expand Up @@ -183,11 +188,13 @@ fn check_complaint() {
"email address violated complaint limit"
);
assert_eq!(error.address, "baz@example.com");
if let Some(bounce) = error.bounce {
if let Some(ref bounce) = error.bounce {
assert_eq!(bounce.bounce_type, BounceType::Complaint);
} else {
assert!(false, "Error::bounce should be set");
}
let failure: Failure = From::from(error);
assert_eq!(failure.0, Status::TooManyRequests);
}
}
}
Expand Down Expand Up @@ -230,6 +237,8 @@ fn check_db_error() {
if let Some(_) = error.bounce {
assert!(false, "Error::bounce should not be set");
}
let failure: Failure = From::from(error);
assert_eq!(failure.0, Status::InternalServerError);
}
}
}
Expand Down