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

Report more details about errors in do function #24

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
20 changes: 15 additions & 5 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/shurcooL/go/ctxhttp"
Expand Down Expand Up @@ -69,27 +70,36 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status: %v", resp.Status)
}

var out struct {
Data *json.RawMessage
Errors errors
//Extensions interface{} // Unused.
}
err = json.NewDecoder(resp.Body).Decode(&out)
if err != nil {
return err
respBodyCopy, errRead := ioutil.ReadAll(resp.Body)
if errRead != nil {
return fmt.Errorf("error decoding output: <an additional error ocurred while reading output: %s>", errRead)
}
return fmt.Errorf("error decoding output: %v (output was: %s)", err, respBodyCopy)
}
if out.Data != nil {
err := jsonutil.UnmarshalGraphQL(*out.Data, v)
if err != nil {
return err
return fmt.Errorf("error unmarshalling data: %s", err)
}
}
if len(out.Errors) > 0 {
return out.Errors
}
if resp.StatusCode != http.StatusOK {
respBodyCopy, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("unexpected status: %v <an additional error ocurred while reading output: %s>", resp.Status, err)
}
return fmt.Errorf("unexpected status: %v %s", resp.Status, respBodyCopy)
}
return nil
}

Expand Down