Skip to content

Commit 43a876d

Browse files
committed
add custom HTTP reply
1 parent 9bf947a commit 43a876d

File tree

1 file changed

+20
-4
lines changed
  • mithril-aggregator/src/http_server/routes

1 file changed

+20
-4
lines changed

mithril-aggregator/src/http_server/routes/router.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ use crate::DependencyManager;
77
use mithril_common::MITHRIL_API_VERSION;
88

99
use reqwest::header::{HeaderMap, HeaderValue};
10+
use reqwest::StatusCode;
1011
use std::sync::Arc;
1112
use warp::http::Method;
12-
use warp::{Filter, Rejection};
13+
use warp::reject::Reject;
14+
use warp::{Filter, Rejection, Reply};
15+
16+
#[derive(Debug)]
17+
pub struct VersionMismatchError;
18+
19+
impl Reject for VersionMismatchError {}
1320

1421
/// Routes
1522
pub fn routes(
@@ -33,9 +40,10 @@ pub fn routes(
3340
.or(signer_routes::routes(dependency_manager.clone()))
3441
.or(signatures_routes::routes(dependency_manager.clone()))
3542
.or(epoch_routes::routes(dependency_manager))
36-
.with(cors)
37-
.with(warp::reply::with::headers(headers)),
43+
.with(cors),
3844
)
45+
.recover(handle_custom)
46+
.with(warp::reply::with::headers(headers))
3947
}
4048

4149
/// API Version verification
@@ -45,8 +53,16 @@ fn header_must_be() -> impl Filter<Extract = (), Error = Rejection> + Copy {
4553
match maybe_header {
4654
None => Ok(()),
4755
Some(version) if version == MITHRIL_API_VERSION => Ok(()),
48-
Some(_version) => Err(warp::reject()),
56+
Some(_version) => Err(warp::reject::custom(VersionMismatchError)),
4957
}
5058
})
5159
.untuple_one()
5260
}
61+
62+
pub async fn handle_custom(reject: Rejection) -> Result<impl Reply, Rejection> {
63+
if reject.find::<VersionMismatchError>().is_some() {
64+
Ok(StatusCode::PRECONDITION_FAILED)
65+
} else {
66+
Err(reject)
67+
}
68+
}

0 commit comments

Comments
 (0)