From 40b59537eb0edcfc8f2732c99da1ea725919b78b Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Tue, 10 May 2022 13:05:04 -0400 Subject: [PATCH] feat(container): automatic debugging on errors (#11915) * feat(container): automatic debugging on errors * tests * group deps * fix test errors Co-authored-by: Aleksandr Bezobchuk --- baseapp/custom_txhandler_test.go | 3 +- container/build.go | 38 ++++++++++- container/container_test.go | 32 ++++++++++ container/debug.go | 106 +++++++++++++++++++++++++++++-- go.mod | 2 - 5 files changed, 169 insertions(+), 12 deletions(-) diff --git a/baseapp/custom_txhandler_test.go b/baseapp/custom_txhandler_test.go index cd6efbbdfd..78e4c8befe 100644 --- a/baseapp/custom_txhandler_test.go +++ b/baseapp/custom_txhandler_test.go @@ -4,9 +4,10 @@ import ( "context" "fmt" + "github.com/tendermint/tendermint/crypto/tmhash" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/tx" - "github.com/tendermint/tendermint/crypto/tmhash" ) type handlerFun func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) diff --git a/container/build.go b/container/build.go index 92edaaff20..b340b2a603 100644 --- a/container/build.go +++ b/container/build.go @@ -9,9 +9,13 @@ package container // Ex: // var x int // Build(Provide(func() int { return 1 }), &x) +// +// Build uses the debug mode provided by AutoDebug which means there will be +// verbose debugging information if there is an error and nothing upon success. +// Use BuildDebug to configure debug behavior. func Build(containerOption Option, outputs ...interface{}) error { loc := LocationFromCaller(1) - return build(loc, nil, containerOption, outputs...) + return build(loc, AutoDebug(), containerOption, outputs...) } // BuildDebug is a version of Build which takes an optional DebugOption for @@ -27,10 +31,38 @@ func build(loc Location, debugOpt DebugOption, option Option, outputs ...interfa return err } + // debug cleanup + defer func() { + for _, f := range cfg.cleanup { + f() + } + }() + + err = doBuild(cfg, loc, debugOpt, option, outputs...) + if err != nil { + if cfg.onError != nil { + err2 := cfg.onError.applyConfig(cfg) + if err2 != nil { + return err2 + } + } + return err + } else { + if cfg.onSuccess != nil { + err2 := cfg.onSuccess.applyConfig(cfg) + if err2 != nil { + return err2 + } + } + return nil + } +} + +func doBuild(cfg *debugConfig, loc Location, debugOpt DebugOption, option Option, outputs ...interface{}) error { defer cfg.generateGraph() // always generate graph on exit if debugOpt != nil { - err = debugOpt.applyConfig(cfg) + err := debugOpt.applyConfig(cfg) if err != nil { return err } @@ -39,7 +71,7 @@ func build(loc Location, debugOpt DebugOption, option Option, outputs ...interfa cfg.logf("Registering providers") cfg.indentLogger() ctr := newContainer(cfg) - err = option.apply(ctr) + err := option.apply(ctr) if err != nil { cfg.logf("Failed registering providers because of: %+v", err) return err diff --git a/container/container_test.go b/container/container_test.go index 6cc8399cfb..e393b91e24 100644 --- a/container/container_test.go +++ b/container/container_test.go @@ -580,3 +580,35 @@ func TestLogging(t *testing.T) { require.NoError(t, err) require.Contains(t, string(graphfileContents), "