Skip to content

Commit

Permalink
fix panic when empty stack trace (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
taxio authored Jan 8, 2024
1 parent f2efcff commit caa564a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func (e *Error) Unwrap() error {
}

func (e *Error) StackTrace() []uintptr {
if e.stack == nil {
return nil
}
return *e.stack
}

Expand Down
12 changes: 12 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ func TestWithStack(t *testing.T) {
}
}
})

t.Run("empty", func(t *testing.T) {
baseErr := stderr.New("base")
err := errors.Wrap(baseErr)
var cErr *errors.Error
if ok := errors.As(err, &cErr); !ok {
t.Fatal("expected error to be *errors.Error")
}
if len(cErr.StackTrace()) != 0 {
t.Errorf("expected no stack trace")
}
})
}

func TestWithAttrs(t *testing.T) {
Expand Down

0 comments on commit caa564a

Please sign in to comment.