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

Support customizing rejections for #[derive(TypedPath)] #1012

Merged
merged 8 commits into from
May 17, 2022

Conversation

davidpdrsn
Copy link
Member

Previously #[derive(TypedPath)] would always use PathRejection as the rejection type. Your only way to customize that was using Result<YourPath, PathRejection> in the handler, which is annoying if you're using it in multiple places.

This makes it possible to use your own rejection instead:

#[derive(TypedPath, Deserialize)]
#[typed_path("/users/:id", rejection(UsersMemberRejection))]
struct UsersMember {
    id: String,
}

struct UsersMemberRejection;

// Your rejection type must implement `From<PathRejection>`.
//
// Here you can grab whatever details from the inner rejection
// that you need
impl From<PathRejection> for UsersMemberRejection {
    fn from(rejection: PathRejection) -> Self {
        // ...
    }
}

// And your rejection must implement `IntoResponse`, like all rejections
impl IntoResponse for UsersMemberRejection {
    fn into_response(self) -> Response {
        // ...
    }
}

@davidpdrsn davidpdrsn enabled auto-merge (squash) May 8, 2022 22:25
@takkuumi
Copy link
Contributor

takkuumi commented May 9, 2022

So excited, I am not sure could implement like this?

#[derive(TypedPath, Deserialize)]
#[typed_path("/users/:id", rejection("the path param for id `{0}` is not a number"))]
struct UsersMember {
    id: String,
}

@davidpdrsn
Copy link
Member Author

davidpdrsn commented May 9, 2022

@NateLing Not quite. Path has more failure cases than that and you'd have to handle all them:

impl From<PathRejection> for UsersMemberRejection {
    fn from(rejection: PathRejection) -> Self {
        match rejection {
            PathRejection::FailedToDeserializePathParams(inner) => match inner.into_kind() {
                // what do you want to do for each of these?
                ErrorKind::WrongNumberOfParameters { got, expected } => ...,
                ErrorKind::ParseErrorAtKey { key, value, expected_type } => ...,
                ErrorKind::ParseErrorAtIndex { index, value, expected_type } => ...,
                ErrorKind::ParseError { value, expected_type } => ...,
                ErrorKind::InvalidUtf8InPathParam { key } => ...,
                ErrorKind::UnsupportedType { name } => ...,
                ErrorKind::Message(_) => ...,
                _ => ...,
            },
            PathRejection::MissingPathParams(inner) => ...,
            _ => ...,
        }
    }
}

axum-extra/src/routing/typed.rs Outdated Show resolved Hide resolved
axum-extra/src/routing/typed.rs Outdated Show resolved Hide resolved
axum-extra/src/routing/typed.rs Outdated Show resolved Hide resolved
axum-macros/src/typed_path.rs Outdated Show resolved Hide resolved
axum-macros/src/typed_path.rs Outdated Show resolved Hide resolved
axum-macros/src/typed_path.rs Outdated Show resolved Hide resolved
davidpdrsn and others added 3 commits May 17, 2022 19:35
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
@davidpdrsn davidpdrsn requested a review from jplatte May 17, 2022 18:57
@davidpdrsn davidpdrsn merged commit 5948cde into main May 17, 2022
@davidpdrsn davidpdrsn deleted the typed-path-customize-rejection branch May 17, 2022 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants