Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jul 28, 2020
1 parent ff7eb03 commit f54d63e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
8 changes: 6 additions & 2 deletions web/contact/testdata/parse_query.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"status": 400,
"response": {
"error": "mismatched input '$' expecting {'(', TEXT, STRING}",
"code": "query_unexpected_token",
"code": "unexpected_token",
"extra": {
"token": "$"
}
Expand All @@ -36,7 +36,11 @@
},
"status": 400,
"response": {
"error": "can't resolve 'birthday' to attribute, scheme or field"
"error": "can't resolve 'birthday' to attribute, scheme or field",
"code": "unknown_property",
"extra": {
"property": "birthday"
}
}
},
{
Expand Down
13 changes: 9 additions & 4 deletions web/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/nyaruka/goflow/contactql"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/utils/jsonx"
"github.com/nyaruka/mailroom/web"

Expand All @@ -12,18 +13,22 @@ import (
)

func TestErrorResponse(t *testing.T) {
// create a simple error
er1 := web.NewErrorResponse(errors.New("I'm an error!"))
assert.Equal(t, "I'm an error!", er1.Error)

er1JSON, err := jsonx.Marshal(er1)
assert.NoError(t, err)
assert.JSONEq(t, `{"error": "I'm an error!"}`, string(er1JSON))

er2 := web.NewErrorResponse(contactql.NewQueryError("I'm a rich error!", "foo_code", map[string]string{"foo": "123"}))
assert.Equal(t, "I'm a rich error!", er2.Error)
assert.Equal(t, "foo_code", er2.Code)
// create a rich error
_, err = contactql.ParseQuery(envs.NewBuilder().Build(), "$$", nil)

er2 := web.NewErrorResponse(err)
assert.Equal(t, "mismatched input '$' expecting {'(', TEXT, STRING}", er2.Error)
assert.Equal(t, "unexpected_token", er2.Code)

er2JSON, err := jsonx.Marshal(er2)
assert.NoError(t, err)
assert.JSONEq(t, `{"error": "I'm a rich error!", "code": "foo_code", "extra": {"foo": "123"}}`, string(er2JSON))
assert.JSONEq(t, `{"error": "mismatched input '$' expecting {'(', TEXT, STRING}", "code": "unexpected_token", "extra": {"token": "$"}}`, string(er2JSON))
}
3 changes: 1 addition & 2 deletions web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ func (s *Server) WrapJSONHandler(handler JSONHandler) http.HandlerFunc {
if serr != nil {
logrus.WithError(err).WithField("http_request", r).Error("error serializing handler response")
w.WriteHeader(http.StatusInternalServerError)
serialized, _ := json.Marshal(errors.New("error serializing handler response"))
w.Write(serialized)
w.Write([]byte(`{"error": "error serializing handler response"}`))
return
}

Expand Down

0 comments on commit f54d63e

Please sign in to comment.