From bd1356c7af3a0c357ece46378dafcd71965a6b46 Mon Sep 17 00:00:00 2001 From: Will Lahti Date: Fri, 20 Jan 2017 12:56:30 -0500 Subject: [PATCH] Add empty string checks to error handling framework This changeset ensures a blank component or reason code will not be used when creating an error. It also checks for the existence of a callstack before proceeding with the callstack message append logic. https://jira.hyperledger.org/browse/FAB-1571 Change-Id: Id8c44373f7d63e65b9c361457602ad780639589a Signed-off-by: Will Lahti --- core/errors/errors.go | 32 +++++++++++++++++--------- core/errors/errors_test.go | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/core/errors/errors.go b/core/errors/errors.go index edb538aa761..fcc6473b0f2 100644 --- a/core/errors/errors.go +++ b/core/errors/errors.go @@ -69,7 +69,7 @@ func newHLError(debug bool) *hlError { func setupHLError(e *hlError, debug bool) { e.componentcode = "UTILITY" e.reasoncode = "UNKNOWNERROR" - e.message = "An unknown error has occurred." + e.message = "An unknown error occurred." if !debug { e.stackGetter = noopGetStack return @@ -109,14 +109,18 @@ func (h *hlError) GetErrorCode() string { // Message returns the corresponding error message for this error in default // language. func (h *hlError) Message() string { - // initialize logging level for errors from core.yaml. it can also be set - // for code running on the peer dynamically via CLI using - // "peer logging setlevel error " - errorLogLevelString, _ := flogging.GetModuleLevel("error") - message := h.GetErrorCode() + " - " + fmt.Sprintf(h.message, h.args...) - if errorLogLevelString == logging.DEBUG.String() { - message = appendCallStack(message, h.GetStack()) + + // check that the error has a callstack before proceeding + if h.GetStack() != "" { + // initialize logging level for errors from core.yaml. it can also be set + // for code running on the peer dynamically via CLI using + // "peer logging setlevel error " + errorLogLevelString, _ := flogging.GetModuleLevel("error") + + if errorLogLevelString == logging.DEBUG.String() { + message = appendCallStack(message, h.GetStack()) + } } return message @@ -143,9 +147,15 @@ func ErrorWithCallstack(componentcode string, reasoncode string, message string, func newCustomError(componentcode string, reasoncode string, message string, generateStack bool, args ...interface{}) CallStackError { e := &hlError{} setupHLError(e, generateStack) - e.componentcode = strings.ToUpper(componentcode) - e.reasoncode = strings.ToUpper(reasoncode) - e.message = message + if componentcode != "" { + e.componentcode = strings.ToUpper(componentcode) + } + if reasoncode != "" { + e.reasoncode = strings.ToUpper(reasoncode) + } + if message != "" { + e.message = message + } e.args = args return e } diff --git a/core/errors/errors_test.go b/core/errors/errors_test.go index 0baa024241a..2401e4bfc19 100644 --- a/core/errors/errors_test.go +++ b/core/errors/errors_test.go @@ -85,6 +85,53 @@ func ExampleError() { // not be appended to the error message flogging.SetModuleLevel("error", "warning") + err := Error("Utility", "UnknownError", "An unknown error occurred.") + + if err != nil { + fmt.Printf("%s\n", err.Error()) + fmt.Printf("%s\n", err.GetErrorCode()) + fmt.Printf("%s\n", err.GetComponentCode()) + fmt.Printf("%s\n", err.GetReasonCode()) + fmt.Printf("%s\n", err.Message()) + // Output: + // UTILITY_UNKNOWNERROR - An unknown error occurred. + // UTILITY_UNKNOWNERROR + // UTILITY + // UNKNOWNERROR + // UTILITY_UNKNOWNERROR - An unknown error occurred. + } +} + +func ExampleError_blankParameters() { + // when the 'error' module is set to anything but debug, the callstack will + // not be appended to the error message + flogging.SetModuleLevel("error", "warning") + + // create error with blank strings for the component code, reason code, and + // message text. the code should use the default for each value instead of + // using the blank strings + err := Error("", "", "") + + if err != nil { + fmt.Printf("%s\n", err.Error()) + fmt.Printf("%s\n", err.GetErrorCode()) + fmt.Printf("%s\n", err.GetComponentCode()) + fmt.Printf("%s\n", err.GetReasonCode()) + fmt.Printf("%s\n", err.Message()) + // Output: + // UTILITY_UNKNOWNERROR - An unknown error occurred. + // UTILITY_UNKNOWNERROR + // UTILITY + // UNKNOWNERROR + // UTILITY_UNKNOWNERROR - An unknown error occurred. + } +} + +func ExampleErrorWithCallstack() { + // when the 'error' module is set to anything but debug, the callstack will + // not be appended to the error message + flogging.SetModuleLevel("error", "warning") + err := ErrorWithCallstack("Utility", "UnknownError", "An unknown error occurred.") if err != nil {