Skip to content

Commit

Permalink
Print more error details in LogAndHandlePanic (#542)
Browse files Browse the repository at this point in the history
Before this change, panics handled by `LogAndHandlePanic` were logged to
the Quesma UI with only the stack trace - without any details about the
error itself (for example without
`runtime error: comparing uncomparable type []string`). This made
debugging panics harder.

This commit adds the error information from `commonRecovery` also to
`LogAndHandlePanic`.

Before:
```
quesma request failed: panic recovered goroutine 3436 [running]:
runtime/debug.Stack() /Users/piotrgrabowski/sdk/go1.23rc1/src/runtime/debug/stack.go:26
+0x64 quesma/quesma/recovery.LogAndHandlePanic({0x1011f2510, 0x14004aaac90}
...
```

After:
<pre>
quesma request failed: Panic recovered: <b>runtime error: comparing
uncomparable type []string</b>
goroutine 3027 [running]: runtime/debug.Stack()
/Users/piotrgrabowski/sdk/go1.23rc1/src/runtime/debug/stack.go:26
+0x64 quesma/quesma/recovery.LogAndHandlePanic({0x102b0a510,
0x14003944330}
...
</pre>

Co-authored-by: Jacek Migdal <[email protected]>
  • Loading branch information
avelanarius and jakozaur authored Jul 18, 2024
1 parent 4e42a9b commit 833bbd9
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions quesma/quesma/recovery/recovery_strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ import (
// make the recovery simple as possible.
var PanicCounter atomic.Int64

func commonRecovery(r any, panicLogger func() *zerolog.Event) {
PanicCounter.Add(1)
var err error
func recoveredToError(r any) error {
switch t := r.(type) {
case string:
err = errors.New(t)
return errors.New(t)
case error:
err = t
return t
default:
err = errors.New("unknown error")
return errors.New("unknown error")
}
panicLogger().Msgf("Panic recovered: %s\n%s", err, string(debug.Stack()))
}

func commonRecovery(r any, panicLogger func() *zerolog.Event) {
PanicCounter.Add(1)
panicLogger().Msgf("Panic recovered: %s\n%s", recoveredToError(r), string(debug.Stack()))
}

func LogPanic() {
Expand All @@ -49,6 +51,6 @@ func LogAndHandlePanic(ctx context.Context, cleanupHandler func(err error)) {
commonRecovery(r, func() *zerolog.Event {
return logger.ErrorWithCtx(ctx)
})
cleanupHandler(errors.New("panic recovered " + string(debug.Stack())))
cleanupHandler(errors.New("Panic recovered: " + recoveredToError(r).Error() + "\n" + string(debug.Stack())))
}
}

0 comments on commit 833bbd9

Please sign in to comment.