Skip to content

Commit

Permalink
Make frames private
Browse files Browse the repository at this point in the history
  • Loading branch information
jfontan committed May 8, 2024
1 parent a092cb2 commit 4995bc2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 7 additions & 2 deletions jerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type JError struct {
parent error
wrap error
values map[string]interface{}
Frames []Frame
frames []Frame
}

// Frame is a stack frame for the error.
Expand All @@ -62,7 +62,7 @@ func (j *JError) New() *JError {
instance: true,
message: j.message,
parent: j,
Frames: fillFrames(stackSkip, stackDepth),
frames: fillFrames(stackSkip, stackDepth),
values: make(map[string]interface{}),
}
}
Expand Down Expand Up @@ -138,6 +138,11 @@ func (j *JError) GetInt(key string) (int, bool) {
return val, ok
}

// Frames returns the stack frames of the error.
func (j *JError) Frames() []Frame {
return j.frames
}

func fillFrames(skip, depth int) []Frame {
if debug {
start := time.Now()
Expand Down
7 changes: 5 additions & 2 deletions jerror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ func TestStack(t *testing.T) {
require := require.New(t)

err := New("stack error").New()
require.Len(err.Frames, 3)
require.Len(err.frames, 3)

last := err.Frames[0]
frames := err.Frames()
last := frames[0]
parts := strings.Split(last.Function, "/")
require.Equal("jerror.TestStack", parts[len(parts)-1])

Expand All @@ -113,6 +114,8 @@ func TestValues(t *testing.T) {
err := orig.New()
err.Set("key", "value")
require.EqualError(err, "values error")

// make sure the original error is not modified
_, ok := orig.Get("key")
require.False(ok)

Expand Down

0 comments on commit 4995bc2

Please sign in to comment.