2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-11-10 11:31:58 +08:00

Include file and line number info in the error generated from a panic

This commit is contained in:
Ken Hibino 2021-01-10 11:44:08 -08:00
parent 0712e90f23
commit a150d18ed7

View File

@ -8,8 +8,10 @@ import (
"context" "context"
"fmt" "fmt"
"math/rand" "math/rand"
"runtime"
"runtime/debug" "runtime/debug"
"sort" "sort"
"strings"
"sync" "sync"
"time" "time"
@ -345,7 +347,19 @@ func (p *processor) perform(ctx context.Context, task *Task) (err error) {
defer func() { defer func() {
if x := recover(); x != nil { if x := recover(); x != nil {
p.logger.Errorf("recovering from panic. See the stack trace below for details:\n%s", string(debug.Stack())) p.logger.Errorf("recovering from panic. See the stack trace below for details:\n%s", string(debug.Stack()))
err = fmt.Errorf("panic: %v", x) _, file, line, ok := runtime.Caller(1) // skip the first frame (panic itself)
if ok && strings.Contains(file, "runtime/") {
// The panic came from the runtime, most likely due to incorrect
// map/slice usage. The parent frame should have the real trigger.
_, file, line, ok = runtime.Caller(2)
}
// Include the file and line number info in the error, if runtime.Caller returned ok.
if ok {
err = fmt.Errorf("panic [%s:%d]: %v", file, line, x)
} else {
err = fmt.Errorf("panic: %v", x)
}
} }
}() }()
return p.handler.ProcessTask(ctx, task) return p.handler.ProcessTask(ctx, task)