hasura / go-graphql-client

Package graphql provides a GraphQL client implementation.
MIT License
395 stars 91 forks source link

returns NetworkError when the request response returns a non-200 http status code #143

Closed LukeSparkLayer closed 2 months ago

LukeSparkLayer commented 2 months ago

This PR adds a new error type: NetworkError

This error contains the status code and response body which were previously returned as the error message.

This change has two benefits:

// Before 
err := client.Query(ctx, query, variables)
if err != nil {
    serviceErrRegex := regexp.MustCompile(`^non-200 OK status code: 5\d\d.+`)
    if serviceErrRegex.MatchString(err.Error()) {
        // handle 5xx status response
    }
}
// After
err := client.Query(ctx, query, variables)
statusErr := graphql.NetworkError{}
if errors.As(err, &statusErr) && statusErr.StatusCode() >= 500 && statusErr.StatusCode() < 600 {
    // handle 5xx status response
}
LukeSparkLayer commented 2 months ago

Thanks for your quick reviews @hgiasac. I have added the changes suggested. Let me know if there's any others needed.

hgiasac commented 2 months ago

@LukeSparkLayer the change looks good. However, you need to fix the error message in unit tests as well. We can merge after all tests are passed