@@ -7,9 +7,16 @@ use crate::DependencyManager;
7
7
use mithril_common:: MITHRIL_API_VERSION ;
8
8
9
9
use reqwest:: header:: { HeaderMap , HeaderValue } ;
10
+ use reqwest:: StatusCode ;
10
11
use std:: sync:: Arc ;
11
12
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 { }
13
20
14
21
/// Routes
15
22
pub fn routes (
@@ -33,9 +40,10 @@ pub fn routes(
33
40
. or ( signer_routes:: routes ( dependency_manager. clone ( ) ) )
34
41
. or ( signatures_routes:: routes ( dependency_manager. clone ( ) ) )
35
42
. or ( epoch_routes:: routes ( dependency_manager) )
36
- . with ( cors)
37
- . with ( warp:: reply:: with:: headers ( headers) ) ,
43
+ . with ( cors) ,
38
44
)
45
+ . recover ( handle_custom)
46
+ . with ( warp:: reply:: with:: headers ( headers) )
39
47
}
40
48
41
49
/// API Version verification
@@ -45,8 +53,16 @@ fn header_must_be() -> impl Filter<Extract = (), Error = Rejection> + Copy {
45
53
match maybe_header {
46
54
None => Ok ( ( ) ) ,
47
55
Some ( version) if version == MITHRIL_API_VERSION => Ok ( ( ) ) ,
48
- Some ( _version) => Err ( warp:: reject ( ) ) ,
56
+ Some ( _version) => Err ( warp:: reject:: custom ( VersionMismatchError ) ) ,
49
57
}
50
58
} )
51
59
. untuple_one ( )
52
60
}
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