Skip to content

Commit

Permalink
Handle HTTP errors (#15)
Browse files Browse the repository at this point in the history
* Add HTTP error handling to NetworkQueryer

* Add check to prevent nil pointer dereference
  • Loading branch information
greenmato authored Feb 24, 2021
1 parent 035738b commit 8e743a2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func IntrospectAPI(queryer Queryer) (*ast.Schema, error) {
}

// if we dont have a name on the response
if remoteSchema.QueryType.Name == "" {
if remoteSchema == nil || remoteSchema.QueryType.Name == "" {
return nil, errors.New("Could not find the root query")
}

Expand Down
6 changes: 6 additions & 0 deletions queryer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"net/http"
"reflect"
"strconv"

"github.com/vektah/gqlparser/v2/ast"
)
Expand Down Expand Up @@ -164,6 +165,11 @@ func (q *NetworkQueryer) sendRequest(acc *http.Request) ([]byte, error) {
}
defer resp.Body.Close()

// check for HTTP errors
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return body, errors.New("response was not successful with status code: " + strconv.Itoa(resp.StatusCode))
}

// we're done
return body, err
}
Expand Down

0 comments on commit 8e743a2

Please sign in to comment.