Skip to content

Commit 237d9a3

Browse files
committed
feat: on panic recovery, make the stacktrace start at the right oops encapsulation level
1 parent f506fa9 commit 237d9a3

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

builder.go

+12
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ func (o OopsErrorBuilder) Recover(cb func()) (err error) {
147147
} else {
148148
err = o.Wrap(fmt.Errorf("%v", r))
149149
}
150+
151+
// without this, the stacktrace would have start to the Wrap() call
152+
e := err.(OopsError)
153+
if len(e.stacktrace.frames) > 0 { // just for safety, should always be true
154+
e.stacktrace.frames = e.stacktrace.frames[1:]
155+
}
150156
}
151157
}()
152158

@@ -163,6 +169,12 @@ func (o OopsErrorBuilder) Recoverf(cb func(), msg string, args ...any) (err erro
163169
} else {
164170
err = o.Wrapf(o.Errorf("%v", r), msg, args...)
165171
}
172+
173+
// without this, the stacktrace would have start to the Wrapf() call
174+
e := err.(OopsError)
175+
if len(e.stacktrace.frames) > 0 { // just for safety, should always be true
176+
e.stacktrace.frames = e.stacktrace.frames[1:]
177+
}
166178
}
167179
}()
168180

go.work

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ use (
1111
./examples/slog
1212
./examples/logrus
1313
./examples/sources
14+
./examples/panic
1415
)

0 commit comments

Comments
 (0)