Adding few extra errors never hurt anyone.

This commit is contained in:
2021-03-27 19:14:17 +00:00
parent 7f7f666ccc
commit 97d3682ed9
3 changed files with 13 additions and 12 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ prepare:
go get github.com/go-critic/go-critic/cmd/gocritic
test:
gocritic check
gocritic check -disable=unslice
go test -race $(ADDITIONAL_BUILD_FLAGS) -v -coverprofile coverage.txt -covermode=atomic
lint:
+10 -9
View File
@@ -17,29 +17,30 @@ type requestBase struct {
Variables interface{} `json:"variables"`
}
func queryBuilder(data string, variables interface{}) []byte {
func queryBuilder(data string, variables interface{}) ([]byte, error) {
var err error
var qb requestBase
qb.Query = data
qb.Variables = variables
j := new(bytes.Buffer)
j2, _ := json.Marshal(qb)
if err := json.Compact(j, j2); err != nil {
fmt.Println(err)
panic("Unable to process the query")
return []byte{}, errors.New(fmt.Sprintf("Unable to process specified query. Check if it's valid: %s", err.Error()))
}
// Attemt to decrease size of the generated JSON
compactedBuffer := new(bytes.Buffer)
err := json.Compact(compactedBuffer, j.Bytes())
err = json.Compact(compactedBuffer, j.Bytes())
if err != nil {
fmt.Println(err)
panic("Unable to compress json")
return []byte{}, errors.New(fmt.Sprintf("Unable to compress your query: %s", err.Error()))
}
return []byte(compactedBuffer.String())
return []byte(compactedBuffer.String()), err
}
func Query(query string, variables interface{}, headers map[string]interface{}) (string, error) {
var err error
readyQuery := queryBuilder(query, variables)
readyQuery, err := queryBuilder(query, variables)
if err != nil {
return "", err
}
req := fasthttp.AcquireRequest()
req.Header.SetContentType("application/json")
req.Header.Set("Accept-Encoding", "gzip")
+2 -2
View File
@@ -40,8 +40,8 @@ func Test_queryBuilder(t *testing.T) {
for _, tt := range tests {
assert := assert.New(t)
t.Run(tt.name, func(t *testing.T) {
gets := string(queryBuilder(tt.args.data, tt.args.variables))
assert.Equal(tt.want, gets, fmt.Sprintf("Unexpected query output in test %s", tt.name))
gets, _ := queryBuilder(tt.args.data, tt.args.variables)
assert.Equal(tt.want, string(gets), fmt.Sprintf("Unexpected query output in test %s", tt.name))
},
)
}