Skip to content

Commit

Permalink
add custom HTTP reply
Browse files Browse the repository at this point in the history
  • Loading branch information
ghubertpalo committed Dec 13, 2022
1 parent 6af68fd commit d74bca1
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions mithril-aggregator/src/http_server/routes/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ use crate::DependencyManager;
use mithril_common::MITHRIL_API_VERSION;

use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::StatusCode;
use std::sync::Arc;
use warp::http::Method;
use warp::{Filter, Rejection};
use warp::reject::Reject;
use warp::{Filter, Rejection, Reply};

#[derive(Debug)]
pub struct VersionMismatchError;

impl Reject for VersionMismatchError {}

/// Routes
pub fn routes(
Expand All @@ -33,9 +40,10 @@ pub fn routes(
.or(signer_routes::routes(dependency_manager.clone()))
.or(signatures_routes::routes(dependency_manager.clone()))
.or(epoch_routes::routes(dependency_manager))
.with(cors)
.with(warp::reply::with::headers(headers)),
.with(cors),
)
.recover(handle_custom)
.with(warp::reply::with::headers(headers))
}

/// API Version verification
Expand All @@ -45,8 +53,16 @@ fn header_must_be() -> impl Filter<Extract = (), Error = Rejection> + Copy {
match maybe_header {
None => Ok(()),
Some(version) if version == MITHRIL_API_VERSION => Ok(()),
Some(_version) => Err(warp::reject()),
Some(_version) => Err(warp::reject::custom(VersionMismatchError)),
}
})
.untuple_one()
}

pub async fn handle_custom(reject: Rejection) -> Result<impl Reply, Rejection> {
if reject.find::<VersionMismatchError>().is_some() {
Ok(StatusCode::PRECONDITION_FAILED)
} else {
Err(reject)
}
}

0 comments on commit d74bca1

Please sign in to comment.