Skip to content

Commit

Permalink
feat: Include original error when event cannot be encoded as JSON (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhcarvalho committed Jul 7, 2020
1 parent 7c5f125 commit e7c66ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
6 changes: 4 additions & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ func getRequestBodyFromEvent(event *Event) []byte {
return body
}

partialMarshallMessage := "Original event couldn't be marshalled. Succeeded by stripping the data " +
"that uses interface{} type. Please verify that the data you attach to the scope is serializable."
partialMarshallMessage := fmt.Sprintf("Could not encode original event as JSON. "+
"Succeeded by removing Breadcrumbs, Contexts and Extra. "+
"Please verify the data you attach to the scope. "+
"Error: %s", err)
// Try to serialize the event, with all the contextual data that allows for interface{} stripped.
event.Breadcrumbs = nil
event.Contexts = nil
Expand Down
18 changes: 10 additions & 8 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ type unserializableType struct {
UnsupportedField func()
}

const basicEvent = "{\"message\":\"mkey\",\"sdk\":{},\"user\":{}}"
const enhancedEvent = "{\"extra\":{\"info\":\"Original event couldn't be marshalled. Succeeded by stripping " +
"the data that uses interface{} type. Please verify that the data you attach to the scope is serializable.\"}," +
"\"message\":\"mkey\",\"sdk\":{},\"user\":{}}"
//nolint: lll
const (
basicEvent = `{"message":"mkey","sdk":{},"user":{}}`
enhancedEventInvalidBreadcrumb = `{"extra":{"info":"Could not encode original event as JSON. Succeeded by removing Breadcrumbs, Contexts and Extra. Please verify the data you attach to the scope. Error: json: error calling MarshalJSON for type *sentry.Event: json: error calling MarshalJSON for type *sentry.Breadcrumb: json: unsupported type: func()"},"message":"mkey","sdk":{},"user":{}}`
enhancedEventInvalidContextOrExtra = `{"extra":{"info":"Could not encode original event as JSON. Succeeded by removing Breadcrumbs, Contexts and Extra. Please verify the data you attach to the scope. Error: json: error calling MarshalJSON for type *sentry.Event: json: unsupported type: func()"},"message":"mkey","sdk":{},"user":{}}`
)

func TestGetRequestBodyFromEventValid(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Expand All @@ -47,7 +49,7 @@ func TestGetRequestBodyFromEventInvalidBreadcrumbsField(t *testing.T) {
})

got := string(body)
want := enhancedEvent
want := enhancedEventInvalidBreadcrumb

if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
Expand All @@ -63,7 +65,7 @@ func TestGetRequestBodyFromEventInvalidExtraField(t *testing.T) {
})

got := string(body)
want := enhancedEvent
want := enhancedEventInvalidContextOrExtra

if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
Expand All @@ -79,7 +81,7 @@ func TestGetRequestBodyFromEventInvalidContextField(t *testing.T) {
})

got := string(body)
want := enhancedEvent
want := enhancedEventInvalidContextOrExtra

if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
Expand All @@ -103,7 +105,7 @@ func TestGetRequestBodyFromEventMultipleInvalidFields(t *testing.T) {
})

got := string(body)
want := enhancedEvent
want := enhancedEventInvalidBreadcrumb

if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
Expand Down

0 comments on commit e7c66ce

Please sign in to comment.