From 833bbd9e7f96c16288d1fa27aadf8817384db05b Mon Sep 17 00:00:00 2001 From: Piotr Grabowski Date: Thu, 18 Jul 2024 14:46:49 +0200 Subject: [PATCH] Print more error details in LogAndHandlePanic (#542) 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:
quesma request failed: Panic recovered: runtime error: comparing
uncomparable type []string
goroutine 3027 [running]: runtime/debug.Stack()
/Users/piotrgrabowski/sdk/go1.23rc1/src/runtime/debug/stack.go:26
+0x64 quesma/quesma/recovery.LogAndHandlePanic({0x102b0a510,
0x14003944330}
...
Co-authored-by: Jacek Migdal --- quesma/quesma/recovery/recovery_strategies.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/quesma/quesma/recovery/recovery_strategies.go b/quesma/quesma/recovery/recovery_strategies.go index 00cea1fca..955d2ea31 100644 --- a/quesma/quesma/recovery/recovery_strategies.go +++ b/quesma/quesma/recovery/recovery_strategies.go @@ -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() { @@ -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()))) } }