Skip to content

Allow custom errors to be returned from queries, mutations #205

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

Merged
merged 5 commits into from
Jul 13, 2018

Conversation

pepsighan
Copy link
Contributor

Fixes #40.

This is not a breaking change, as FieldResult still works. For use cases which desire for custom error handling that return additional response fields ergnomically, those error types should implement IntoFieldError trait which converts your error type to the FieldError. This maintains that in the response, message field is always returned along with other optional user desired metadata.

You may implement your error as:

enum CustomError {
  NotFound
}

impl IntoFieldError for CustomError {
  fn into_field_error(self) -> FieldError {
    FieldError::new("Not Found", graphql_value!({ "type": "NOT_FOUND" })
  }
}

And use it likewise:

field add_likes(&executor, id: i32) -> Result<Like, CustomError> {
  Err(CustomError::NotFound)
}

@LegNeato
Copy link
Member

LegNeato commented Jul 10, 2018

Thanks for the change! Have you seen #196 / https://github.com/facebook/graphql/releases/tag/June2018? There are a lot more requirements for errors in the newest spec. Does the newest spec obsolete this PR? For example, graphql/graphql-spec#407.

Direct link to new error section: http://facebook.github.io/graphql/June2018/#sec-Errors

@pepsighan
Copy link
Contributor Author

@LegNeato I read the error section of the specification and one of the change it outlines is to use extensions field to map out additional metadata to the error. We are currently using data field in its place which needs to be refactored and that could be done in another PR.

As for this PR, it only deals with mapping our own error to the FieldError. So, I think the spec does not have a problem with it.

Copy link
Member

@LegNeato LegNeato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, looks reasonable. Thanks for the test as well. Can you add it to the changelog please?

@LegNeato LegNeato merged commit f115d0b into graphql-rust:master Jul 13, 2018
@LegNeato
Copy link
Member

Great, thanks! 🍻 . Sorry this took so long to merge.

@pepsighan pepsighan deleted the error_handling branch January 14, 2019 03:47
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.

2 participants