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

feat: custom error type in axum request extractor #945

Merged
merged 3 commits into from Jun 11, 2022

Conversation

goodbin
Copy link
Contributor

@goodbin goodbin commented Jun 7, 2022

Ability to specify a custom type of error in axum request extractor.

Example:

enum CustomError {
    Parse(ParseRequestError),
    Custom(String),
}

impl From<ParseRequestError> for CustomError {
    fn from(err: ParseRequestError) -> Self {
        Self::Parse(err)
    }
}

impl IntoResponse for CustomError {
    fn into_response(self) -> Response {
        match self {
            CustomError::Parse(e) => (StatusCode::PAYLOAD_TOO_LARGE, e.to_string()).into_response(),
            CustomError::Custom(e) => (StatusCode::INTERNAL_SERVER_ERROR, e).into_response(),
        }
    }
}

async fn graphql_handler(
    schema: Extension<StarWarsSchema>,
    req: GraphQLRequest<CustomError>,
) -> GraphQLResponse {
    schema.execute(req.into_inner()).await.into()
}

rejection::GraphQLRejection is used by default

GoodBin added 3 commits June 7, 2022 18:13
Ability to specify a custom type of error in axum request extractor.
@sunli829
Copy link
Collaborator

Thanks! 🙂

@sunli829 sunli829 merged commit 34c8a51 into async-graphql:master Jun 11, 2022
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

2 participants