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 anyhow::Error (#988) #1215

Merged
merged 4 commits into from
Nov 15, 2023
Merged

Support anyhow::Error (#988) #1215

merged 4 commits into from
Nov 15, 2023

Conversation

tyranron
Copy link
Member

Resolves #988

Synopsis

See #988:

anyhow is a dominant error handling library for rust. Currently, there is no convenient way to use anyhow Errs in juniper code. For example,

struct Mutation;

#[juniper::graphql_object]
impl Mutation {
  async fn Foo(line_number: i32) -> FieldResult<bool> {
    anyhow::ensure!(line_number >= 0);
    Ok(true)
  }
}

doesn't work.

IntoFieldResult offers a way of translating custom error types into FieldResults, but unfortunately implementing IntoFieldResult for anyhow::Error is not possible due to https://doc.rust-lang.org/error-index.html#E0117.

Solution

Provide IntoFieldError implementation for anyhow::Error behind anyhow Cargo feature.

Backtrace

Write anyhow::Error::backtrace() into FieldError::extensions() as backtrace field, if anyhow library supports it. Controllable by backtrace Cargo feature in the same manner as anyhow crate does.

@tyranron tyranron added k::api Related to API (application interface) k::integration Related to integration with third-party libraries or systems feature New feature or request lib::anyhow Related to `anyhow` crate integration labels Nov 15, 2023
@tyranron tyranron added this to the 0.16.0 milestone Nov 15, 2023
@tyranron tyranron self-assigned this Nov 15, 2023
@tyranron tyranron marked this pull request as ready for review November 15, 2023 17:54
@tyranron tyranron merged commit 316b188 into master Nov 15, 2023
174 checks passed
@tyranron tyranron deleted the anyhow-error branch November 15, 2023 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request k::api Related to API (application interface) k::integration Related to integration with third-party libraries or systems lib::anyhow Related to `anyhow` crate integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IntoFieldResult for anyhow::Error
1 participant