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 Error Extensions #9

Closed
nicolaiunrein opened this issue Mar 28, 2020 · 8 comments · Fixed by #10
Closed

Support Error Extensions #9

nicolaiunrein opened this issue Mar 28, 2020 · 8 comments · Fixed by #10

Comments

@nicolaiunrein
Copy link
Collaborator

Hey,
first of all, I really enjoy using your library so far. I think it is already very comprehensive. One thing I am missing though is returning extensions for errors as described here in the spec. Something like:

  "errors": [
    {
      "message": "Name for character with ID 1002 could not be fetched.",
      "locations": [ { "line": 6, "column": 7 } ],
      "path": [ "hero", "heroFriends", 1, "name" ],
      "extensions": {
        "code": "CAN_NOT_FETCH_BY_ID",
        "timestamp": "Fri Feb 9 14:33:09 UTC 2018"
      }
    }
  ]
}

In terms of api, I think it would be nice to have a trait with a single method with_extensions which you could implement on your errors. Something like:

// somewhere in async-graphql
pub trait ErrorExtensions {
    fn with_extensions(&self) -> Option<serde_json::Value> {
        None
    }
}

impl<T: Into<Error>> ErrorExtensions for T {}
// somewhere in users code...
impl ErrorExtensions for MyError {
  fn with_extensions(&self) -> Option<serde_json::Value> {

    let extensions = serde_json::json!({
       "code": self.code.to_string(),
       "timestamp": get_current_timestamp()
    });

    Some(extensions)
  }
}

I am happy to contribute, so if you want any help just let me know.

@sunli829
Copy link
Collaborator

Thank you for your advice, that's a great idea, and I really need your help. These days I want to optimize the code in the subscription to make it easier to use, so our work doesn't conflict.If you have time, you can help implement `ErrorExtensions'.😁
Thanks again!

@nicolaiunrein
Copy link
Collaborator Author

I thought a little about it and started implementing a first draft. Will be ready for review tomorrow or on monday I think.

@sunli829
Copy link
Collaborator

sunli829 commented Mar 29, 2020

Is a wrapper similar to PositionError?

@nicolaiunrein
Copy link
Collaborator Author

Is a wrapper similar to PositionError?

Yes exactly. See PR for details.

@sunli829
Copy link
Collaborator

sunli829 commented Apr 2, 2020

I reimplemented the error type in branch error-type, the error extension is still not very good, give some advice?

@nicolaiunrein
Copy link
Collaborator Author

I looked at the error-type and made some adjustments. Please have a look and also at the example uses cases. It turns out to be quite tricky to implement some sort of default implementation but still let the user implement custom behaviour. Feel free to undo the commit if you don't like it.

@nicolaiunrein
Copy link
Collaborator Author

I also replaced the trait bound std::err::Error with std::fmt::Display because I think Error is an unnecessary restriction.

@sunli829
Copy link
Collaborator

sunli829 commented Apr 2, 2020

Thank you, It's merged to master.😁

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 a pull request may close this issue.

2 participants