octocrab icon indicating copy to clipboard operation
octocrab copied to clipboard

Handle GraphQL query errors

Open anowell opened this issue 4 years ago • 2 comments

I found myself getting deserialization errors like this using the graphql helper (despite propagating errors with ?). I didn't anticipate having to handle errors myself.

Object({"data": Object({"repository": Null}), "errors": Array([Object({"locations": Array([Object({"column": Number(15), "line": Number(2)})]), "message": String("Could not resolve to a Repository with the name \'/millardjn/alumina\'."), "path": Array([String("repository")]), "type": String("NOT_FOUND")})])})

Since this is the standard GraphQL way of returning query errors, my suggestion would be to add a GraphqlErrors type and internally make the request and deserialize into a type like below and automatically converting the Error variant into the actual octocrab::Error type.

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum GraphqlResponse<T> {
    // order matters for untagged, since T could be something generic like serde_json::Value
    Error(GraphqlErrors),
    Value(T),
}

anowell avatar Mar 12 '21 06:03 anowell

Thank you for your issue! If anyone wants to make a PR adding handling for graphql errors, feel free.

XAMPPRocky avatar Mar 12 '21 06:03 XAMPPRocky

good to know. no promises here, but since I'm working around this, I may yet submit a PR.

anowell avatar Mar 12 '21 06:03 anowell