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

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

Merged
merged 5 commits into from
Jun 21, 2024

Conversation

LukeSparkLayer
Copy link

@LukeSparkLayer LukeSparkLayer commented Jun 20, 2024

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:

  • Allows systems that depend on this package to be able to check for error response codes by using go's inbuilt errors package and matching on the error code, rather than string matching on the error message.
  • Returns the body of the request as an io.Reader to reduce unnecessary data being passed around.
// 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
}

graphql.go Outdated Show resolved Hide resolved
graphql.go Outdated Show resolved Hide resolved
graphql.go Outdated Show resolved Hide resolved
graphql.go Outdated Show resolved Hide resolved
@LukeSparkLayer LukeSparkLayer requested a review from hgiasac June 20, 2024 15:07
graphql.go Outdated Show resolved Hide resolved
graphql.go Show resolved Hide resolved
@LukeSparkLayer LukeSparkLayer changed the title returns ErrResponseStatusNotOK when the request response returns a non-200 http status code returns NetworkError when the request response returns a non-200 http status code Jun 21, 2024
@LukeSparkLayer
Copy link
Author

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

@LukeSparkLayer LukeSparkLayer requested a review from hgiasac June 21, 2024 15:16
@hgiasac hgiasac marked this pull request as ready for review June 21, 2024 15:17
@hgiasac
Copy link

hgiasac commented Jun 21, 2024

@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

Copy link

@hgiasac hgiasac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for contributing

@hgiasac hgiasac merged commit fa97047 into hasura:master Jun 21, 2024
1 check failed
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 this pull request may close these issues.

2 participants