2022-08-29 18:45:37 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime/debug"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
|
|
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
|
|
|
)
|
|
|
|
|
2022-09-22 14:01:10 +00:00
|
|
|
// Recover handles panics and reports to Sentry.
|
2022-08-29 18:45:37 +00:00
|
|
|
func Recover(ctx context.Context) {
|
2022-09-22 14:01:10 +00:00
|
|
|
if err := recover(); err != nil {
|
|
|
|
panicStack := string(debug.Stack())
|
|
|
|
if eventID := sentry.CurrentHub().Recover(err); eventID != nil {
|
|
|
|
ctx.Logger().Info("panic captured", "event_id", *eventID)
|
|
|
|
}
|
2023-06-15 00:24:31 +00:00
|
|
|
ctx.Logger().Error(fmt.Errorf("panic"), panicStack,
|
2022-11-15 22:48:02 +00:00
|
|
|
"recover", err,
|
|
|
|
)
|
2022-09-22 14:01:10 +00:00
|
|
|
if !sentry.Flush(time.Second * 5) {
|
|
|
|
ctx.Logger().Info("sentry flush failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecoverWithExit handles panics and reports to Sentry before exiting.
|
|
|
|
func RecoverWithExit(ctx context.Context) {
|
2022-08-29 18:45:37 +00:00
|
|
|
if err := recover(); err != nil {
|
|
|
|
panicStack := string(debug.Stack())
|
|
|
|
if eventID := sentry.CurrentHub().Recover(err); eventID != nil {
|
|
|
|
ctx.Logger().Info("panic captured", "event_id", *eventID)
|
|
|
|
}
|
2022-11-15 22:48:02 +00:00
|
|
|
ctx.Logger().Error(fmt.Errorf("panic"), "recovered from panic before exiting",
|
|
|
|
"stack-trace", panicStack,
|
|
|
|
"recover", err,
|
|
|
|
)
|
2022-08-29 18:45:37 +00:00
|
|
|
if !sentry.Flush(time.Second * 5) {
|
|
|
|
ctx.Logger().Info("sentry flush failed")
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|