1
1
#[ cfg( test) ]
2
2
mod tests;
3
3
4
- use rocket:: { Request , Route , Catcher , route, catcher} ;
4
+ use rocket:: { Request , Route , Catcher , route, catcher, outcome :: Outcome } ;
5
5
use rocket:: data:: { Data , ToByteUnit } ;
6
6
use rocket:: http:: { Status , Method :: { Get , Post } } ;
7
7
use rocket:: response:: { Responder , status:: Custom } ;
8
- use rocket:: outcome:: { try_outcome, IntoOutcome } ;
9
8
use rocket:: tokio:: fs:: File ;
10
9
11
10
fn forward < ' r > ( _req : & ' r Request , data : Data < ' r > ) -> route:: BoxFuture < ' r > {
@@ -25,12 +24,17 @@ fn name<'r>(req: &'r Request, _: Data<'r>) -> route::BoxFuture<'r> {
25
24
}
26
25
27
26
fn echo_url < ' r > ( req : & ' r Request , _: Data < ' r > ) -> route:: BoxFuture < ' r > {
28
- let param_outcome = req. param :: < & str > ( 1 )
29
- . and_then ( Result :: ok)
30
- . or_error ( Status :: BadRequest ) ;
31
-
32
27
Box :: pin ( async move {
33
- route:: Outcome :: from ( req, try_outcome ! ( param_outcome) )
28
+ let param_outcome = match req. param :: < & str > ( 1 ) {
29
+ Some ( Ok ( v) ) => v,
30
+ Some ( Err ( e) ) => return Outcome :: Error ( (
31
+ Status :: BadRequest ,
32
+ Box :: new ( e) as catcher:: ErasedError
33
+ ) ) ,
34
+ None => return Outcome :: Error ( ( Status :: BadRequest , catcher:: default_error_type ( ) ) ) ,
35
+ } ;
36
+
37
+ route:: Outcome :: from ( req, param_outcome)
34
38
} )
35
39
}
36
40
@@ -62,9 +66,11 @@ fn get_upload<'r>(req: &'r Request, _: Data<'r>) -> route::BoxFuture<'r> {
62
66
route:: Outcome :: from ( req, std:: fs:: File :: open ( path) . ok ( ) ) . pin ( )
63
67
}
64
68
65
- fn not_found_handler < ' r > ( _: Status , req : & ' r Request ) -> catcher:: BoxFuture < ' r > {
69
+ fn not_found_handler < ' r > ( _: Status , req : & ' r Request , _e : catcher:: ErasedError < ' r > )
70
+ -> catcher:: BoxFuture < ' r >
71
+ {
66
72
let responder = Custom ( Status :: NotFound , format ! ( "Couldn't find: {}" , req. uri( ) ) ) ;
67
- Box :: pin ( async move { responder. respond_to ( req) } )
73
+ Box :: pin ( async move { responder. respond_to ( req) . map_err ( |s| ( s , _e ) ) } )
68
74
}
69
75
70
76
#[ derive( Clone ) ]
@@ -82,11 +88,17 @@ impl CustomHandler {
82
88
impl route:: Handler for CustomHandler {
83
89
async fn handle < ' r > ( & self , req : & ' r Request < ' _ > , data : Data < ' r > ) -> route:: Outcome < ' r > {
84
90
let self_data = self . data ;
85
- let id = req. param :: < & str > ( 0 )
86
- . and_then ( Result :: ok)
87
- . or_forward ( ( data, Status :: NotFound ) ) ;
88
-
89
- route:: Outcome :: from ( req, format ! ( "{} - {}" , self_data, try_outcome!( id) ) )
91
+ let id = match req. param :: < & str > ( 1 ) {
92
+ Some ( Ok ( v) ) => v,
93
+ Some ( Err ( e) ) => return Outcome :: Forward ( ( data, Status :: BadRequest , Box :: new ( e) ) ) ,
94
+ None => return Outcome :: Forward ( (
95
+ data,
96
+ Status :: BadRequest ,
97
+ catcher:: default_error_type ( )
98
+ ) ) ,
99
+ } ;
100
+
101
+ route:: Outcome :: from ( req, format ! ( "{} - {}" , self_data, id) )
90
102
}
91
103
}
92
104
0 commit comments