Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Include original error when event cannot be encoded as JSON #258

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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