Skip to content

Commit

Permalink
- refactored print function
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Mar 24, 2023
1 parent bf67210 commit cfaaaca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
10 changes: 3 additions & 7 deletions trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,11 @@ func getNodeFromDBAndDecode(n []byte, db common.DBWriteCacher, marshalizer marsh
}

func treatLogError(logInstance logger.Logger, err error, key []byte) {
logLevel := logger.LogTrace
extraInfo := make([]interface{}, 0, 6)
extraInfo = append(extraInfo, "error", err, "key", key)
if !errors.IsClosingError(err) {
logLevel = logger.LogWarning
extraInfo = append(extraInfo, "stack trace", string(debug.Stack()))
if logInstance.GetLevel() != logger.LogTrace {
return
}

logInstance.Log(logLevel, common.GetNodeFromDBErrorString, extraInfo...)
logInstance.Trace(common.GetNodeFromDBErrorString, "error", err, "key", key, "stack trace", string(debug.Stack()))
}

func resolveIfCollapsed(n node, pos byte, db common.DBWriteCacher) error {
Expand Down
53 changes: 19 additions & 34 deletions trie/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/multiversx/mx-chain-core-go/core/atomic"
"github.com/multiversx/mx-chain-go/common"
dataMock "github.com/multiversx/mx-chain-go/dataRetriever/mock"
mxErrors "github.com/multiversx/mx-chain-go/errors"
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/multiversx/mx-chain-go/trie/keyBuilder"
logger "github.com/multiversx/mx-chain-logger-go"
Expand Down Expand Up @@ -631,57 +629,44 @@ func TestShouldStopIfContextDoneBlockingIfBusy(t *testing.T) {
func TestTreatLogError(t *testing.T) {
t.Parallel()

t.Run("error is not of type of closing error", func(t *testing.T) {
t.Run("logger instance is not in Trace mode, should not call", func(t *testing.T) {
t.Parallel()

key := []byte("key")
err := errors.New("trie was not found")
wasCalled := false
logInstance := &testscommon.LoggerStub{
LogCalled: func(logLevel logger.LogLevel, message string, args ...interface{}) {
wasCalled = true
require.Equal(t, logger.LogWarning, logLevel)
require.Equal(t, common.GetNodeFromDBErrorString, message)
require.Equal(t, 6, len(args))
expectedFirst5Args := []interface{}{"error", err, "key", key, "stack trace"}
require.Equal(t, expectedFirst5Args, args[:5])
GetLevelCalled: func() logger.LogLevel {
return logger.LogDebug
},
TraceCalled: func(message string, args ...interface{}) {
assert.Fail(t, "should have not called Log")
},
}

treatLogError(logInstance, err, key)
assert.True(t, wasCalled)
treatLogError(log, err, key) //display only
})
t.Run("error is of type of closing error", func(t *testing.T) {
t.Run("logger instance is in Trace mode, should call", func(t *testing.T) {
t.Parallel()

key := []byte("key")
numCalled := 0
var err error

wasCalled := false
err := errors.New("error")
logInstance := &testscommon.LoggerStub{
LogCalled: func(logLevel logger.LogLevel, message string, args ...interface{}) {
numCalled++
require.Equal(t, logger.LogTrace, logLevel)
GetLevelCalled: func() logger.LogLevel {
return logger.LogTrace
},
TraceCalled: func(message string, args ...interface{}) {
wasCalled = true
require.Equal(t, common.GetNodeFromDBErrorString, message)
require.Equal(t, 4, len(args))
expectedFirst5Args := []interface{}{"error", err, "key", key}
require.Equal(t, expectedFirst5Args, args)
require.Equal(t, 6, len(args))
expectedFirst5Args := []interface{}{"error", err, "key", key, "stack trace"}
require.Equal(t, expectedFirst5Args, args[:5])
},
}

t.Run("db is closed", func(t *testing.T) {
crtCounter := numCalled
err = storage.ErrDBIsClosed
treatLogError(logInstance, err, key)
assert.Equal(t, crtCounter+1, numCalled)
})
t.Run("context closing", func(t *testing.T) {
crtCounter := numCalled
err = mxErrors.ErrContextClosing
treatLogError(logInstance, err, key)
assert.Equal(t, crtCounter+1, numCalled)
})
treatLogError(logInstance, err, key)
assert.True(t, wasCalled)
})
}

Expand Down

0 comments on commit cfaaaca

Please sign in to comment.