Skip to content

Commit

Permalink
Merge pull request #144 from monzo/typhon-terrors
Browse files Browse the repository at this point in the history
Make sure Encode and Decode functions return terrors errors
  • Loading branch information
suhailpatel authored Jan 31, 2022
2 parents e98837c + 325943e commit cead950
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
20 changes: 19 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/monzo/terrors"
"io/ioutil"
"math"
"strings"
"testing"

Expand Down Expand Up @@ -119,6 +121,16 @@ func TestRequestDecodeLegacyProtoMaskingAsJSON(t *testing.T) {
assert.Equal(t, "Hello world!", g.Message)
}

func TestRequestDecodeErrorGivesTerror(t *testing.T) {
req := NewRequest(nil, "GET", "/", nil)
req.Body = ioutil.NopCloser(strings.NewReader("invalid json"))

bout := map[string]string{}
err := req.Decode(&bout)
assert.True(t, terrors.Is(err, "bad_request"))
assert.True(t, terrors.Matches(err, "invalid character 'i'"))
}

// TestRequestEncodeReader verifies that passing an io.Reader to request.Encode() uses it properly as the body, and
// does not attempt to encode it as JSON
func TestRequestEncodeReader(t *testing.T) {
Expand Down Expand Up @@ -170,7 +182,6 @@ func TestRequestEncodeProtobuf(t *testing.T) {

assert.Equal(t, "application/protobuf", req.Header.Get("Content-Type"))
assert.EqualValues(t, bodyBytes, protoContentForComparison)

}

func TestRequestEncodeJSON(t *testing.T) {
Expand All @@ -192,6 +203,13 @@ func TestRequestEncodeJSON(t *testing.T) {
assert.EqualValues(t, bodyBytes, jsonContentForComparison)
}

func TestRequestEncodeErrorGivesTerror(t *testing.T) {
req := NewRequest(nil, "GET", "/", nil)
req.Encode(math.Inf(1))
assert.True(t, terrors.Is(req.err, "internal_service"))
assert.True(t, terrors.Matches(req.err, "unsupported value"))
}

func TestRequestSetMetadata(t *testing.T) {
t.Parallel()

Expand Down
1 change: 1 addition & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (r *Response) Decode(v interface{}) error {
err = json.Unmarshal(b, v)
}

err = terrors.WrapWithCode(err, nil, terrors.ErrBadRequest)
if err != nil {
r.Error = err
}
Expand Down
18 changes: 18 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/monzo/typhon/legacyprototest"
"io"
"io/ioutil"
"math"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -258,6 +259,16 @@ func TestResponseDecodeLegacyProtobufWithAltType(t *testing.T) {
assert.EqualValues(t, 1, gout.Priority)
}

func TestResponseDecodeErrorGivesTerror(t *testing.T) {
rsp := NewResponse(Request{})
rsp.Body = ioutil.NopCloser(strings.NewReader("invalid json"))

bout := map[string]string{}
err := rsp.Decode(&bout)
assert.True(t, terrors.Is(err, "bad_request"))
assert.True(t, terrors.Matches(err, "invalid character 'i'"))
}

// rc is a helper type used in tests involving a generic io.ReadCloser
type rc struct {
strings.Reader
Expand Down Expand Up @@ -390,3 +401,10 @@ func TestResponseEncodeReader(t *testing.T) {
require.NoError(t, err)
assert.Subset(t, body, []byte("hello"), "'hello' should appear in the wire format")
}

func TestResponseEncodeErrorGivesTerror(t *testing.T) {
rsp := NewResponse(Request{})
rsp.Encode(math.Inf(1))
assert.True(t, terrors.Is(rsp.Error, "internal_service"))
assert.True(t, terrors.Matches(rsp.Error, "unsupported value"))
}

0 comments on commit cead950

Please sign in to comment.