Skip to content

Commit

Permalink
Add empty string checks to error handling framework
Browse files Browse the repository at this point in the history
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 <wtlahti@us.ibm.com>
  • Loading branch information
wlahti committed Jan 20, 2017
1 parent 051229a commit bd1356c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
32 changes: 21 additions & 11 deletions core/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <log-level>"
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 <log-level>"
errorLogLevelString, _ := flogging.GetModuleLevel("error")

if errorLogLevelString == logging.DEBUG.String() {
message = appendCallStack(message, h.GetStack())
}
}

return message
Expand All @@ -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
}
Expand Down
47 changes: 47 additions & 0 deletions core/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bd1356c

Please sign in to comment.