Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pub fn to get a Response from a Rejection #827

Closed
wants to merge 2 commits into from
Closed

Add pub fn to get a Response from a Rejection #827

wants to merge 2 commits into from

Conversation

jtroo
Copy link

@jtroo jtroo commented Mar 29, 2021

This is inspired from my desire to easily turn a Rejection into a Response so that my recover handling code is easier. There are some routes where I want to turn all rejections into replies and not fall back to other routes, and I don't want to handle all the built-in rejections manually.

Example:

/// My custom error type
pub enum Error {
    // ... snip
}

impl warp::reject::Reject for Error {}

pub async fn handle_rejection(
    err: Rejection
) -> std::result::Result<warp::reply::Response, std::convert::Infallible> {
    if let Some(e) = err.find::<Error>() {
        // handle my custom rejection
    } else {
        Ok(err.default_response())
    }
}

My original comment:
#712 (comment)

@jtroo
Copy link
Author

jtroo commented Mar 29, 2021

Could also change this part of the rejections example to use this fn. Any thoughts?

    } 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";
    }

@jtroo
Copy link
Author

jtroo commented Mar 30, 2021

Just realized that there was another open PR that accomplishes the same thing but using the Into trait: #820

Either one would work for me. The approach in this PR has the benefit of the documentation to warn users about the behaviour of custom rejections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant