This commit is contained in:
Alex Sharov 2021-10-22 09:41:20 +07:00 committed by GitHub
parent ec0155d4ed
commit 505bf97249
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 25 deletions

View File

@ -1,32 +1,10 @@
package dbg
import (
"fmt"
stack2 "github.com/go-stack/stack"
)
func StackInLogsFormat() string {
// Stack returns stack-trace in logger-friendly compact formatting
func Stack() string {
return stack2.Trace().TrimBelow(stack2.Caller(1)).String()
}
// Recover - does save panic to datadir/crashreports, bud doesn't log to logger and doesn't stop the process
// it returns recovered panic as error in format friendly for our logger
// common pattern of use - assign to named output param:
// func A() (err error) {
// defer func() { err = debug.Recover(err) }() // avoid crash because Erigon's core does many things
// }
func Recover(err error) error {
panicResult := recover()
if panicResult == nil {
return err
}
switch typed := panicResult.(type) {
case error:
err = fmt.Errorf("%w, trace: %s", typed, StackInLogsFormat())
default:
err = fmt.Errorf("%+v, trace: %s", typed, StackInLogsFormat())
}
return err
}

View File

@ -207,7 +207,7 @@ func (f *Fetch) receiveMessage(ctx context.Context, sentryClient sentry.SentryCl
func (f *Fetch) handleInboundMessage(ctx context.Context, req *sentry.InboundMessage, sentryClient sentry.SentryClient) (err error) {
defer func() {
if rec := recover(); rec != nil {
err = fmt.Errorf("%+v, trace: %s", rec, dbg.StackInLogsFormat())
err = fmt.Errorf("%+v, trace: %s, rlp: %x", rec, dbg.Stack(), req.Data)
}
}()