Skip to content

Commit

Permalink
Return errors alongside result instead of just an error
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnStarich committed Aug 20, 2024
1 parent 0801ced commit a81d441
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 32 deletions.
14 changes: 5 additions & 9 deletions queryer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,12 @@ type QueryerFunc func(*QueryInput) (interface{}, error)
// Query invokes the provided function and writes the response to the receiver
func (q QueryerFunc) Query(ctx context.Context, input *QueryInput, receiver interface{}) error {
// invoke the handler
response, err := q(input)
if err != nil {
return err
response, responseErr := q(input)
if response != nil {
// assume the mock is writing the same kind as the receiver
reflect.ValueOf(receiver).Elem().Set(reflect.ValueOf(response))
}

// assume the mock is writing the same kind as the receiver
reflect.ValueOf(receiver).Elem().Set(reflect.ValueOf(response))

// no errors
return nil
return responseErr // support partial success: always return the queryer error after setting the return data
}

type NetworkQueryer struct {
Expand Down
11 changes: 4 additions & 7 deletions queryerMultiOp.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ func (q *MultiOpQueryer) Query(ctx context.Context, input *QueryInput, receiver
}

// format the result as needed
err = q.queryer.ExtractErrors(unmarshaled)
if err != nil {
return err
}

// assign the result under the data key to the receiver
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Expand All @@ -86,9 +81,11 @@ func (q *MultiOpQueryer) Query(ctx context.Context, input *QueryInput, receiver
if err != nil {
return err
}
if err := decoder.Decode(unmarshaled["data"]); err != nil {
return err
}

// the only way for things to go wrong now happen while decoding
return decoder.Decode(unmarshaled["data"])
return q.queryer.ExtractErrors(unmarshaled)
}

func (q *MultiOpQueryer) loadQuery(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
Expand Down
15 changes: 7 additions & 8 deletions queryerNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package graphql
import (
"context"
"encoding/json"
"github.com/mitchellh/mapstructure"
"net/http"

"github.com/mitchellh/mapstructure"
)

// SingleRequestQueryer sends the query to a url and returns the response
Expand Down Expand Up @@ -80,11 +81,6 @@ func (q *SingleRequestQueryer) Query(ctx context.Context, input *QueryInput, rec
return err
}

// otherwise we have to copy the response onto the receiver
if err = q.queryer.ExtractErrors(result); err != nil {
return err
}

// assign the result under the data key to the receiver
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Expand All @@ -93,7 +89,10 @@ func (q *SingleRequestQueryer) Query(ctx context.Context, input *QueryInput, rec
if err != nil {
return err
}
if err = decoder.Decode(result["data"]); err != nil {
return err
}

// the only way for things to go wrong now happen while decoding
return decoder.Decode(result["data"])
// finally extract errors, if any, and return them
return q.queryer.ExtractErrors(result) // TODO add unit tests!
}
11 changes: 3 additions & 8 deletions queryer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,10 @@ func TestQueryerWithMiddlewares(t *testing.T) {
for _, row := range queryerTable {
t.Run(row.name, func(t *testing.T) {
t.Run("Middleware Failures", func(t *testing.T) {
someErr := errors.New("This One")
queryer := row.queryer.WithMiddlewares([]NetworkMiddleware{
func(r *http.Request) error {
return errors.New("This One")
return someErr
},
})

Expand All @@ -381,13 +382,7 @@ func TestQueryerWithMiddlewares(t *testing.T) {

// fire the query
err := queryer.Query(context.Background(), input, &map[string]interface{}{})
if err == nil {
t.Error("Did not enounter an error when we should have")
return
}
if err.Error() != "This One" {
t.Errorf("Did not encountered expected error message: Expected 'This One', found %v", err.Error())
}
assert.ErrorIs(t, err, someErr)
})

t.Run("Middlware success", func(t *testing.T) {
Expand Down

0 comments on commit a81d441

Please sign in to comment.