pub async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible> {
    let code;
    let message;

    if err.is_not_found() {
        code = StatusCode::NOT_FOUND;
        message = "NOT_FOUND";
    } else if let Some(e) = err.find::<warp::filters::body::BodyDeserializeError>() {
        // This error happens if the body could not be deserialized correctly
        // We can use the cause to analyze the error and customize the error message
        message = match e.source() {
            Some(cause) => {
                if cause.to_string().contains("denom") {
                    "FIELD_ERROR: denom"
                } else {
                    "BAD_REQUEST"
                }
            }
            None => "BAD_REQUEST",
        };
        code = StatusCode::BAD_REQUEST;
    } else if let Some(_) = err.find::<warp::reject::MethodNotAllowed>() {
        // We can handle a specific error, here METHOD_NOT_ALLOWED,
        // and render it however we want
        code = StatusCode::METHOD_NOT_ALLOWED;
        message = "METHOD_NOT_ALLOWED";
    } else {
        // We should have expected this... Just log and say its a 500
        eprintln!("unhandled rejection: {:?}", err);
        code = StatusCode::INTERNAL_SERVER_ERROR;
        message = "UNHANDLED_REJECTION";
    }

    let json = warp::reply::json(&ErrorMessage {
        code: code.as_u16(),
        message: message.into(),
    });

    Ok(warp::reply::with_status(json, code))
}
// This function receives a `Rejection` and tries to return a custom
// value, otherwise simply passes the rejection along.
This function receives a `Rejection` and tries to return a custom
value, otherwise simply passes the rejection along.
This function receives a ` Rejection ` and tries to return a custom value otherwise simply passes the rejection along .
pub async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible> {
    let code;
    let message;
    if err.is_not_found() {
        code = StatusCode::NOT_FOUND;
        message = "NOT_FOUND";
    } else if let Some(e) = err.find::<warp::filters::body::BodyDeserializeError>() {
        // This error happens if the body could not be deserialized correctly
        // We can use the cause to analyze the error and customize the error message
        message = match e.source() {
            Some(cause) => {
                if cause.to_string().contains("denom") {
                    "FIELD_ERROR: denom"
                } else {
                    "BAD_REQUEST"
                }
            }
            None => "BAD_REQUEST",
        };
        code = StatusCode::BAD_REQUEST;
    } else if let Some(_) = err.find::<warp::reject::MethodNotAllowed>() {
        // We can handle a specific error, here METHOD_NOT_ALLOWED,
        // and render it however we want
        code = StatusCode::METHOD_NOT_ALLOWED;
        message = "METHOD_NOT_ALLOWED";
    } else {
        // We should have expected this... Just log and say its a 500
        eprintln!("unhandled rejection: {:?}", err);
        code = StatusCode::INTERNAL_SERVER_ERROR;
        message = "UNHANDLED_REJECTION";
    }
    let json = warp::reply::json(&ErrorMessage {
        code: code.as_u16(),
        message: message.into(),
    });
    Ok(warp::reply::with_status(json, code))
}
pub async fn handle_rejection ( err : Rejection ) -> Result < impl Reply , Infallible > { let code ; let message ; if err . is_not_found ( ) { code = StatusCode :: NOT_FOUND ; message = " " ; } else if let Some ( e ) = err . find :: < warp :: filters :: body :: BodyDeserializeError > ( ) { // This error happens if the body could not be deserialized correctly // We can use the cause to analyze the error and customize the error message message = match e . source ( ) { Some ( cause ) => { if cause . to_string ( ) . contains ( " " ) { " " } else { " " } } None => " " , } ; code = StatusCode :: BAD_REQUEST ; } else if let Some ( _ ) = err . find :: < warp :: reject :: MethodNotAllowed > ( ) { // We can handle a specific error, here METHOD_NOT_ALLOWED, // and render it however we want code = StatusCode :: METHOD_NOT_ALLOWED ; message = " " ; } else { // We should have expected this... Just log and say its a 500 eprintln ! ( " " err ) ; code = StatusCode :: INTERNAL_SERVER_ERROR ; message = " " ; } let json = warp :: reply :: json ( & ErrorMessage { code : code . as_u16 ( ) , message : message . into ( ) , } ) ; Ok ( warp :: reply :: with_status ( json , code ) ) }
This function receives a `Rejection` and tries to return a custom
value, otherwise simply passes the rejection along.
This function receives a ` Rejection ` and tries to return a custom value otherwise simply passes the rejection along .
err
Rejection