Skip to content

Commit

Permalink
[FAB-3431] Always display error callstack in logs
Browse files Browse the repository at this point in the history
This CR removes the dependency on core.yaml for setting the display
of the callstack, resulting in all calls to Error() appending the
callstack (if available). This ensures the logs will always contain
the callstack. It also modifies the Message() function to only display
the error message (without callstack) for cases where just the message
message is desired. Finally, it refactors the unit tests to avoid the
previously large amount of duplicated code between test cases.

Change-Id: I379a3258fd2b357f2b5504896ff8f2ad4d5e7788
Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
  • Loading branch information
wlahti committed Apr 26, 2017
1 parent 5f91834 commit 3493be3
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 349 deletions.
32 changes: 18 additions & 14 deletions common/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"regexp"
"runtime"

"github.com/hyperledger/fabric/common/flogging"
logging "github.com/op/go-logging"
)

Expand Down Expand Up @@ -73,9 +72,18 @@ func setupCallError(e *callError, generateStack bool) {
e.stack = stack[:length]
}

// Error comes from the error interface
// Error comes from the error interface - it returns the error message and
// appends the callstack, if available
func (e *callError) Error() string {
return e.Message()
message := e.GetErrorCode() + " - " + fmt.Sprintf(e.message, e.args...)
// check that the error has a callstack before proceeding
if e.GetStack() != "" {
message = appendCallStack(message, e.GetStack())
}
if e.prevErr != nil {
message += "\nCaused by: " + e.prevErr.Error()
}
return message
}

// GetStack returns the call stack as a string
Expand All @@ -102,18 +110,14 @@ func (e *callError) GetErrorCode() string {
// language.
func (e *callError) Message() string {
message := e.GetErrorCode() + " - " + fmt.Sprintf(e.message, e.args...)
// check that the error has a callstack before proceeding
if e.GetStack() != "" {
// stacktrace is enabled when `logging.error` in core.yaml is set to
// DEBUG. it can also be toggled for code running on the peer dynamically
// via CLI using `peer logging setlevel error <log-level>`
errorLevel := flogging.GetModuleLevel("error")
if errorLevel == logging.DEBUG.String() {
message = appendCallStack(message, e.GetStack())
}
}

if e.prevErr != nil {
message += "\nCaused by: " + e.prevErr.Error()
switch previousError := e.prevErr.(type) {
case CallStackError:
message += "\nCaused by: " + previousError.Message()
default:
message += "\nCaused by: " + e.prevErr.Error()
}
}
return message
}
Expand Down
Loading

0 comments on commit 3493be3

Please sign in to comment.