Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit 89356e6

Browse files
committed
Adding Stringer implementation for Stacktrace & StacktraceFrame
1 parent 1cc47a9 commit 89356e6

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

stacktrace.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package raven
77

88
import (
99
"bytes"
10+
"fmt"
1011
"go/build"
1112
"io/ioutil"
1213
"path/filepath"
@@ -49,6 +50,24 @@ type StacktraceFrame struct {
4950
InApp bool `json:"in_app"`
5051
}
5152

53+
// Returns a string representation of the stacktrace similar do debug.Stack()
54+
func (s Stacktrace) String() string {
55+
buff := &bytes.Buffer{}
56+
for i := len(s.Frames) - 1; i >= 0; i-- {
57+
f := s.Frames[i]
58+
buff.WriteString(f.String())
59+
buff.WriteString("\n")
60+
}
61+
return buff.String()
62+
}
63+
64+
// Returns a string representation of the frame similar do debug.Stack()
65+
// /absolute/path/to/file.go:37
66+
// funcName: lineContext("the line of code")
67+
func (f StacktraceFrame) String() string {
68+
return fmt.Sprintf("%s:%d\n\t%s: %s", trimPath(f.Filename), f.Lineno, f.Function, f.ContextLine)
69+
}
70+
5271
// Intialize and populate a new stacktrace, skipping skip frames.
5372
//
5473
// context is the number of surrounding lines that should be included for context.

stacktrace_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,37 @@ func TestNewStacktrace_outOfBounds(t *testing.T) {
133133
t.Errorf("incorrect ContextLine: %#v", f.ContextLine)
134134
}
135135
}
136+
137+
func TestStacktraceFrameString(t *testing.T) {
138+
st := trace()
139+
str := st.Frames[len(st.Frames)-1].String()
140+
if !strings.Contains(str, "github.com/getsentry/raven-go/stacktrace_test.go:85") {
141+
t.Errorf("frame.String() does not contain file and line no %s", str)
142+
}
143+
144+
if !strings.Contains(str, "trace: return NewStacktrace(0, 2, []string{thisPackage})") {
145+
t.Errorf("frame.String() does not contain function and line context %s", str)
146+
}
147+
}
148+
149+
func TestStacktraceString(t *testing.T) {
150+
st := trace()
151+
arr := strings.Split(st.String(), "\n")
152+
153+
if len(arr) != 7 {
154+
t.Errorf("incorrect length: %d", len(arr))
155+
}
156+
157+
if !strings.Contains(arr[0], "github.com/getsentry/raven-go/stacktrace_test.go:85") {
158+
t.Errorf("unexpected 1st line from st.String(): %s", arr[0])
159+
}
160+
if !strings.Contains(arr[1], "trace: return NewStacktrace(0, 2, []string{thisPackage})") {
161+
t.Errorf("unexpected 2nd line from st.String(): %s", arr[1])
162+
}
163+
if !strings.Contains(arr[2], "github.com/getsentry/raven-go/stacktrace_test.go:150") {
164+
t.Errorf("unexpected 3rd line from st.String(): %s", arr[2])
165+
}
166+
if !strings.Contains(arr[3], "TestStacktraceString: st := trace()") {
167+
t.Errorf("unexpected 4th line from st.String(): %s", arr[3])
168+
}
169+
}

0 commit comments

Comments
 (0)