Skip to content

Commit 3493be3

Browse files
committed
[FAB-3431] Always display error callstack in logs
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 <[email protected]>
1 parent 5f91834 commit 3493be3

File tree

7 files changed

+115
-349
lines changed

7 files changed

+115
-349
lines changed

common/errors/errors.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"regexp"
2424
"runtime"
2525

26-
"github.com/hyperledger/fabric/common/flogging"
2726
logging "github.com/op/go-logging"
2827
)
2928

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

76-
// Error comes from the error interface
75+
// Error comes from the error interface - it returns the error message and
76+
// appends the callstack, if available
7777
func (e *callError) Error() string {
78-
return e.Message()
78+
message := e.GetErrorCode() + " - " + fmt.Sprintf(e.message, e.args...)
79+
// check that the error has a callstack before proceeding
80+
if e.GetStack() != "" {
81+
message = appendCallStack(message, e.GetStack())
82+
}
83+
if e.prevErr != nil {
84+
message += "\nCaused by: " + e.prevErr.Error()
85+
}
86+
return message
7987
}
8088

8189
// GetStack returns the call stack as a string
@@ -102,18 +110,14 @@ func (e *callError) GetErrorCode() string {
102110
// language.
103111
func (e *callError) Message() string {
104112
message := e.GetErrorCode() + " - " + fmt.Sprintf(e.message, e.args...)
105-
// check that the error has a callstack before proceeding
106-
if e.GetStack() != "" {
107-
// stacktrace is enabled when `logging.error` in core.yaml is set to
108-
// DEBUG. it can also be toggled for code running on the peer dynamically
109-
// via CLI using `peer logging setlevel error <log-level>`
110-
errorLevel := flogging.GetModuleLevel("error")
111-
if errorLevel == logging.DEBUG.String() {
112-
message = appendCallStack(message, e.GetStack())
113-
}
114-
}
113+
115114
if e.prevErr != nil {
116-
message += "\nCaused by: " + e.prevErr.Error()
115+
switch previousError := e.prevErr.(type) {
116+
case CallStackError:
117+
message += "\nCaused by: " + previousError.Message()
118+
default:
119+
message += "\nCaused by: " + e.prevErr.Error()
120+
}
117121
}
118122
return message
119123
}

0 commit comments

Comments
 (0)