Skip to content

Commit 049a3c1

Browse files
committed
Simplify/standardize error handling framework codes
This changeset removes the unnecessary complication of having ComponentCode and ReasonCode types, instead using strings. It then standardizes those strings to upper case and prepends it to all error messages. https://jira.hyperledger.org/browse/FAB-1571 Change-Id: I53a448a3afb143262663ebe93a620cb22e66a215 Signed-off-by: Will Lahti <[email protected]>
1 parent 6ef96a1 commit 049a3c1

File tree

2 files changed

+32
-37
lines changed

2 files changed

+32
-37
lines changed

core/errors/errors.go

+17-22
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"fmt"
2222
"runtime"
23+
"strings"
2324

2425
"github.com/hyperledger/fabric/common/flogging"
2526
logging "github.com/op/go-logging"
@@ -28,12 +29,6 @@ import (
2829
// MaxCallStackLength is the maximum length of the stored call stack
2930
const MaxCallStackLength = 30
3031

31-
// ComponentCode shows the originating component/module
32-
type ComponentCode string
33-
34-
// ReasonCode for low level error description
35-
type ReasonCode string
36-
3732
var errorLogger = logging.MustGetLogger("error")
3833

3934
// CallStackError is a general interface for
@@ -42,8 +37,8 @@ type CallStackError interface {
4237
error
4338
GetStack() string
4439
GetErrorCode() string
45-
GetComponentCode() ComponentCode
46-
GetReasonCode() ReasonCode
40+
GetComponentCode() string
41+
GetReasonCode() string
4742
Message() string
4843
}
4944

@@ -56,8 +51,8 @@ type callstack []uintptr
5651
// create something more useful
5752
type hlError struct {
5853
stack callstack
59-
componentcode ComponentCode
60-
reasoncode ReasonCode
54+
componentcode string
55+
reasoncode string
6156
message string
6257
args []interface{}
6358
stackGetter func(callstack) string
@@ -72,8 +67,8 @@ func newHLError(debug bool) *hlError {
7267
}
7368

7469
func setupHLError(e *hlError, debug bool) {
75-
e.componentcode = "Utility"
76-
e.reasoncode = "UnknownError"
70+
e.componentcode = "UTILITY"
71+
e.reasoncode = "UNKNOWNERROR"
7772
e.message = "An unknown error has occurred."
7873
if !debug {
7974
e.stackGetter = noopGetStack
@@ -97,18 +92,18 @@ func (h *hlError) GetStack() string {
9792
}
9893

9994
// GetComponentCode returns the component name
100-
func (h *hlError) GetComponentCode() ComponentCode {
95+
func (h *hlError) GetComponentCode() string {
10196
return h.componentcode
10297
}
10398

10499
// GetReasonCode returns the reason code - i.e. why the error occurred
105-
func (h *hlError) GetReasonCode() ReasonCode {
100+
func (h *hlError) GetReasonCode() string {
106101
return h.reasoncode
107102
}
108103

109104
// GetErrorCode returns a formatted error code string
110105
func (h *hlError) GetErrorCode() string {
111-
return fmt.Sprintf("%s-%s", h.componentcode, h.reasoncode)
106+
return fmt.Sprintf("%s_%s", h.componentcode, h.reasoncode)
112107
}
113108

114109
// Message returns the corresponding error message for this error in default
@@ -119,7 +114,7 @@ func (h *hlError) Message() string {
119114
// "peer logging setlevel error <log-level>"
120115
errorLogLevelString, _ := flogging.GetModuleLevel("error")
121116

122-
message := fmt.Sprintf(h.message, h.args...)
117+
message := h.GetErrorCode() + " - " + fmt.Sprintf(h.message, h.args...)
123118
if errorLogLevelString == logging.DEBUG.String() {
124119
message = appendCallStack(message, h.GetStack())
125120
}
@@ -135,23 +130,23 @@ func appendCallStack(message string, callstack string) string {
135130

136131
// Error creates a CallStackError using a specific Component Code and
137132
// Reason Code (no callstack is recorded)
138-
func Error(componentcode ComponentCode, reasoncode ReasonCode, message string, args ...interface{}) CallStackError {
133+
func Error(componentcode string, reasoncode string, message string, args ...interface{}) CallStackError {
139134
return newCustomError(componentcode, reasoncode, message, false, args...)
140135
}
141136

142137
// ErrorWithCallstack creates a CallStackError using a specific Component Code and
143138
// Reason Code and fills its callstack
144-
func ErrorWithCallstack(componentcode ComponentCode, reasoncode ReasonCode, message string, args ...interface{}) CallStackError {
139+
func ErrorWithCallstack(componentcode string, reasoncode string, message string, args ...interface{}) CallStackError {
145140
return newCustomError(componentcode, reasoncode, message, true, args...)
146141
}
147142

148-
func newCustomError(componentcode ComponentCode, reasoncode ReasonCode, message string, generateStack bool, args ...interface{}) CallStackError {
143+
func newCustomError(componentcode string, reasoncode string, message string, generateStack bool, args ...interface{}) CallStackError {
149144
e := &hlError{}
150145
setupHLError(e, generateStack)
151-
e.componentcode = componentcode
152-
e.reasoncode = reasoncode
153-
e.args = args
146+
e.componentcode = strings.ToUpper(componentcode)
147+
e.reasoncode = strings.ToUpper(reasoncode)
154148
e.message = message
149+
e.args = args
155150
return e
156151
}
157152

core/errors/errors_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ func ExampleError() {
9494
fmt.Printf("%s\n", err.GetReasonCode())
9595
fmt.Printf("%s\n", err.Message())
9696
// Output:
97-
// An unknown error occurred.
98-
// Utility-UnknownError
99-
// Utility
100-
// UnknownError
101-
// An unknown error occurred.
97+
// UTILITY_UNKNOWNERROR - An unknown error occurred.
98+
// UTILITY_UNKNOWNERROR
99+
// UTILITY
100+
// UNKNOWNERROR
101+
// UTILITY_UNKNOWNERROR - An unknown error occurred.
102102
}
103103
}
104104

@@ -118,11 +118,11 @@ func Example_utilityErrorWithArg() {
118118
fmt.Printf("%s\n", err.GetReasonCode())
119119
fmt.Printf("%s\n", err.Message())
120120
// Output:
121-
// An error occurred: arg1
122-
// Utility-ErrorWithArg
123-
// Utility
124-
// ErrorWithArg
125-
// An error occurred: arg1
121+
// UTILITY_ERRORWITHARG - An error occurred: arg1
122+
// UTILITY_ERRORWITHARG
123+
// UTILITY
124+
// ERRORWITHARG
125+
// UTILITY_ERRORWITHARG - An error occurred: arg1
126126
}
127127
}
128128

@@ -142,10 +142,10 @@ func Example_loggingInvalidLevel() {
142142
fmt.Printf("%s\n", err.GetReasonCode())
143143
fmt.Printf("%s\n", err.Message())
144144
// Output:
145-
// Invalid log level provided - invalid
146-
// Logging-InvalidLevel
147-
// Logging
148-
// InvalidLevel
149-
// Invalid log level provided - invalid
145+
// LOGGING_INVALIDLEVEL - Invalid log level provided - invalid
146+
// LOGGING_INVALIDLEVEL
147+
// LOGGING
148+
// INVALIDLEVEL
149+
// LOGGING_INVALIDLEVEL - Invalid log level provided - invalid
150150
}
151151
}

0 commit comments

Comments
 (0)