hasura / go-graphql-client

Package graphql provides a GraphQL client implementation.
MIT License
405 stars 94 forks source link

export Errors and add extensions to allow clients parse the error #22

Closed dbarrosop closed 2 years ago

dbarrosop commented 2 years ago

Right now dealing with errors is not very graceful as you need to rely on parsing err.Error(). Exporting the error and adding extensions isn't ideal either but at least it let's you try to treat hasura errors a bit more gracefully and do things like:

func parseGraphqlError(err error) error {
    var ghErr graphql.Errors
    if errors.As(err, &ghErr) {
        code, ok := ghErr[0].Extensions["code"]
        if !ok {
            return err
        }
        switch code {
        case "access-denied":
            ...
        default:
            return err
        }
    }

    return err
}

allowing you to treat errors you may be interested in.