-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.go
81 lines (73 loc) · 2.4 KB
/
hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package keeper
import (
"errors"
fmt "fmt"
"runtime"
"runtime/debug"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Implements RollappHooks interface in a recoverable manner
// RollappHooks event hooks for rollapp object (noalias)
type RollappRecoverableHooks interface {
BeforeUpdateStateRecoverable(ctx sdk.Context, seqAddr string, rollappId string) (err error) // Must be called when a rollapp's state changes
}
var _ RollappRecoverableHooks = Keeper{}
// BeforeUpdateState - call hook if registered
// incase of panic, drop the state machine change and return the error.
func (k Keeper) BeforeUpdateStateRecoverable(ctx sdk.Context, seqAddr string, rollappId string) (err error) {
if k.GetHooks() != nil {
return panicCatchingHook(
ctx,
func(ctx sdk.Context, arg ...any) {
k.hooks.BeforeUpdateState(ctx, arg[0].(string), arg[1].(string))
},
seqAddr, rollappId)
}
return nil
}
// panicCatchingHook lets us run the hook function hookFn, but if theres an error
// or panic drop the state machine change, log the error and return it.
// If there is no error, proceeds as normal (but with some slowdown due to SDK store weirdness)
func panicCatchingHook(
ctx sdk.Context,
hookFn func(ctx sdk.Context, arg ...any),
args ...any,
) (err error) {
defer func() {
if recovErr := recover(); recovErr != nil {
err = PrintPanicRecoveryError(ctx, recovErr)
}
}()
cacheCtx, write := ctx.CacheContext()
hookFn(cacheCtx, args...)
if err != nil {
ctx.Logger().Error(err.Error())
} else {
// no error, write the output of f
write()
}
return err
}
// PrintPanicRecoveryError error logs the recoveryError, along with the stacktrace, if it can be parsed.
// If not emits them to stdout.
func PrintPanicRecoveryError(ctx sdk.Context, recoveryError interface{}) error {
err := errors.New("panic occurred during execution")
errStackTrace := string(debug.Stack())
switch e := recoveryError.(type) {
case string:
ctx.Logger().Error("Recovering from (string) panic: " + e)
case runtime.Error:
err = e
ctx.Logger().Error("recovered (runtime.Error) panic: " + e.Error())
case error:
err = e
ctx.Logger().Error("recovered (error) panic: " + e.Error())
default:
ctx.Logger().Error("recovered (default) panic. Could not capture logs in ctx, see stdout")
fmt.Println("Recovering from panic ", recoveryError)
debug.PrintStack()
return err
}
ctx.Logger().Error("stack trace: " + errStackTrace)
return err
}