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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,11 @@ func (c *Client) request(ctx context.Context, query string, variables map[string
}

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
hgiasac marked this conversation as resolved.
Show resolved Hide resolved
err := newError(ErrRequestError, fmt.Errorf("%v; body: %q", resp.Status, body))
err := newError(ErrRequestError, ErrResponseStatusNotOK{
body: resp.Body,
status: resp.Status,
statusCode: resp.StatusCode,
})

if c.debug {
err = err.withRequest(request, reqReader)
Expand Down Expand Up @@ -386,6 +389,28 @@ func newError(code string, err error) Error {
}
}

type ErrResponseStatusNotOK struct {
hgiasac marked this conversation as resolved.
Show resolved Hide resolved
body io.ReadCloser
hgiasac marked this conversation as resolved.
Show resolved Hide resolved
status string
hgiasac marked this conversation as resolved.
Show resolved Hide resolved
statusCode int
}

func (e ErrResponseStatusNotOK) Error() string {
return fmt.Sprintf("non-200 OK status code: %s", e.status)
hgiasac marked this conversation as resolved.
Show resolved Hide resolved
}

func (e ErrResponseStatusNotOK) Body() io.ReadCloser {
return e.body
}

func (e ErrResponseStatusNotOK) Status() string {
return e.status
}

func (e ErrResponseStatusNotOK) StatusCode() int {
return e.statusCode
}

func (e Error) withRequest(req *http.Request, bodyReader io.Reader) Error {
internal := e.getInternalExtension()
bodyBytes, err := io.ReadAll(bodyReader)
Expand Down