11#![ deny( warnings) ]
2- extern crate serde;
3- #[ macro_use]
4- extern crate serde_derive;
5- extern crate pretty_env_logger;
6- extern crate warp;
72
83use std:: error:: Error as StdError ;
94use std:: fmt:: { self , Display } ;
105
6+ use serde_derive:: Serialize ;
117use warp:: http:: StatusCode ;
12- use warp:: { Filter , Rejection , Reply } ;
8+ use warp:: { Future , Filter , Rejection , Reply } ;
139
1410#[ derive( Copy , Clone , Debug ) ]
1511enum Error {
@@ -34,49 +30,53 @@ impl Display for Error {
3430
3531impl StdError for Error { }
3632
37- fn main ( ) {
33+ #[ tokio:: main]
34+ async fn main ( ) {
3835 let hello = warp:: path:: end ( ) . map ( warp:: reply) ;
3936
4037 let oops =
41- warp:: path ( "oops" ) . and_then ( || Err :: < StatusCode , _ > ( warp:: reject:: custom ( Error :: Oops ) ) ) ;
38+ warp:: path ( "oops" ) . and_then ( || futures :: future :: err :: < StatusCode , _ > ( warp:: reject:: custom ( Error :: Oops ) ) ) ;
4239
4340 let nope =
44- warp:: path ( "nope" ) . and_then ( || Err :: < StatusCode , _ > ( warp:: reject:: custom ( Error :: Nope ) ) ) ;
41+ warp:: path ( "nope" ) . and_then ( || futures :: future :: err :: < StatusCode , _ > ( warp:: reject:: custom ( Error :: Nope ) ) ) ;
4542
4643 let routes = warp:: get2 ( )
4744 . and ( hello. or ( oops) . or ( nope) )
4845 . recover ( customize_error) ;
4946
50- warp:: serve ( routes) . run ( ( [ 127 , 0 , 0 , 1 ] , 3030 ) ) ;
47+ warp:: serve ( routes) . run ( ( [ 127 , 0 , 0 , 1 ] , 3030 ) ) . await ;
5148}
5249
5350// This function receives a `Rejection` and tries to return a custom
5451// value, othewise simply passes the rejection along.
55- fn customize_error ( err : Rejection ) -> Result < impl Reply , Rejection > {
56- if let Some ( & err) = err. find_cause :: < Error > ( ) {
57- let code = match err {
58- Error :: Nope => StatusCode :: BAD_REQUEST ,
59- Error :: Oops => StatusCode :: INTERNAL_SERVER_ERROR ,
60- } ;
61- let msg = err. to_string ( ) ;
52+ fn customize_error ( err : Rejection ) -> impl Future < Output = Result < impl Reply , Rejection > > {
53+ let err = {
54+ if let Some ( & err) = err. find_cause :: < Error > ( ) {
55+ let code = match err {
56+ Error :: Nope => StatusCode :: BAD_REQUEST ,
57+ Error :: Oops => StatusCode :: INTERNAL_SERVER_ERROR ,
58+ } ;
59+ let msg = err. to_string ( ) ;
6260
63- let json = warp:: reply:: json ( & ErrorMessage {
64- code : code. as_u16 ( ) ,
65- message : msg,
66- } ) ;
67- Ok ( warp:: reply:: with_status ( json, code) )
68- } else if let Some ( _) = err. find_cause :: < warp:: reject:: MethodNotAllowed > ( ) {
69- // We can handle a specific error, here METHOD_NOT_ALLOWED,
70- // and render it however we want
71- let code = StatusCode :: METHOD_NOT_ALLOWED ;
72- let json = warp:: reply:: json ( & ErrorMessage {
73- code : code. as_u16 ( ) ,
74- message : "oops, you aren't allowed to use this method." . into ( ) ,
75- } ) ;
76- Ok ( warp:: reply:: with_status ( json, code) )
77- } else {
78- // Could be a NOT_FOUND, or any other internal error... here we just
79- // let warp use its default rendering.
80- Err ( err)
81- }
61+ let json = warp:: reply:: json ( & ErrorMessage {
62+ code : code. as_u16 ( ) ,
63+ message : msg,
64+ } ) ;
65+ Ok ( warp:: reply:: with_status ( json, code) )
66+ } else if let Some ( _) = err. find_cause :: < warp:: reject:: MethodNotAllowed > ( ) {
67+ // We can handle a specific error, here METHOD_NOT_ALLOWED,
68+ // and render it however we want
69+ let code = StatusCode :: METHOD_NOT_ALLOWED ;
70+ let json = warp:: reply:: json ( & ErrorMessage {
71+ code : code. as_u16 ( ) ,
72+ message : "oops, you aren't allowed to use this method." . into ( ) ,
73+ } ) ;
74+ Ok ( warp:: reply:: with_status ( json, code) )
75+ } else {
76+ // Could be a NOT_FOUND, or any other internal error... here we just
77+ // let warp use its default rendering.
78+ Err ( err)
79+ }
80+ } ;
81+ futures:: future:: ready ( err)
8282}
0 commit comments