Return unexpected body as error message.

Unexpected message within body ( without data ) is an indication of error.
This commit is contained in:
2021-03-28 00:56:36 +00:00
parent bdd29589af
commit ef5d2efd04
+3 -2
View File
@@ -3,6 +3,7 @@ package gql
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
@@ -45,13 +46,13 @@ func Query(query string, variables interface{}, headers map[string]interface{})
req.SetRequestURI(GraphQLUrl) req.SetRequestURI(GraphQLUrl)
res := fasthttp.AcquireResponse() res := fasthttp.AcquireResponse()
if err := fasthttp.Do(req, res); err != nil { if err := fasthttp.Do(req, res); err != nil {
panic("Unable to execute request") return "", err
} }
fasthttp.ReleaseRequest(req) fasthttp.ReleaseRequest(req)
body := res.Body() body := res.Body()
toReturn := gjson.Get(string(body), "data") toReturn := gjson.Get(string(body), "data")
if toReturn.String() == "" { if toReturn.String() == "" {
fmt.Println("Probably not what you expect:", string(body)) err = errors.New(string(body))
return "", err return "", err
} }
fasthttp.ReleaseResponse(res) fasthttp.ReleaseResponse(res)