Skip to content

Commit

Permalink
feat: add support for exception mechanism metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattanderson committed Feb 10, 2023
1 parent f03b31a commit c8777d8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
17 changes: 17 additions & 0 deletions interfaces.go
Expand Up @@ -204,13 +204,30 @@ func NewRequest(r *http.Request) *Request {
}
}

// Mechanism is the mechanism by which an exception was generated and handled.
type Mechanism struct {
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
HelpLink string `json:"help_link,omitempty"`
Handled *bool `json:"handled,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}

// SetUnhandled indicates that the exception is an unhandled exception, i.e.
// from a panic.
func (m *Mechanism) SetUnhandled() {
h := false
m.Handled = &h
}

// Exception specifies an error that occurred.
type Exception struct {
Type string `json:"type,omitempty"` // used as the main issue title
Value string `json:"value,omitempty"` // used as the main issue subtitle
Module string `json:"module,omitempty"`
ThreadID string `json:"thread_id,omitempty"`
Stacktrace *Stacktrace `json:"stacktrace,omitempty"`
Mechanism *Mechanism `json:"mechanism,omitempty"`
}

// SDKMetaData is a struct to stash data which is needed at some point in the SDK's event processing pipeline
Expand Down
49 changes: 49 additions & 0 deletions interfaces_test.go
Expand Up @@ -160,6 +160,55 @@ func TestEventMarshalJSON(t *testing.T) {
}
}

func TestMechanismMarshalJSON(t *testing.T) {
mechanism := &Mechanism{
Type: "some type",
Description: "some description",
HelpLink: "some help link",
Data: map[string]interface{}{
"some data": "some value",
"some numeric data": 12345,
},
}

got, err := json.Marshal(mechanism)
if err != nil {
t.Fatal(err)
}

want := `{"type":"some type","description":"some description","help_link":"some help link",` +
`"data":{"some data":"some value","some numeric data":12345}}`

if diff := cmp.Diff(want, string(got)); diff != "" {
t.Errorf("Event mismatch (-want +got):\n%s", diff)
}
}

func TestMechanismMarshalJSON_withHandled(t *testing.T) {
mechanism := &Mechanism{
Type: "some type",
Description: "some description",
HelpLink: "some help link",
Data: map[string]interface{}{
"some data": "some value",
"some numeric data": 12345,
},
}
mechanism.SetUnhandled()

got, err := json.Marshal(mechanism)
if err != nil {
t.Fatal(err)
}

want := `{"type":"some type","description":"some description","help_link":"some help link",` +
`"handled":false,"data":{"some data":"some value","some numeric data":12345}}`

if diff := cmp.Diff(want, string(got)); diff != "" {
t.Errorf("Event mismatch (-want +got):\n%s", diff)
}
}

func TestStructSnapshots(t *testing.T) {
testSpan := &Span{
TraceID: TraceIDFromHex("d6c4f03650bd47699ec65c84352b6208"),
Expand Down

0 comments on commit c8777d8

Please sign in to comment.