Skip to content

Commit 6ef96a1

Browse files
committed
Simplify error handling framework messages
This changeset removes the error handling framework's use of an error message map and any code that was in place to add future language translations. This simplification will allow easier adoption of internationalization packages whenever that is deemed a priority. https://jira.hyperledger.org/browse/FAB-1571 Change-Id: I0ae562ebdc10d43c99d5099039671769199af95e Signed-off-by: Will Lahti <[email protected]>
1 parent aaa998e commit 6ef96a1

File tree

5 files changed

+43
-150
lines changed

5 files changed

+43
-150
lines changed

core/errors/errors.go

+21-36
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package errors
1818

1919
import (
2020
"bytes"
21-
"encoding/json"
2221
"fmt"
2322
"runtime"
2423

@@ -46,24 +45,6 @@ type CallStackError interface {
4645
GetComponentCode() ComponentCode
4746
GetReasonCode() ReasonCode
4847
Message() string
49-
MessageIn(string) string
50-
}
51-
52-
type errormap map[string]map[string]map[string]string
53-
54-
var emap errormap
55-
56-
const language string = "en"
57-
58-
func init() {
59-
initErrors()
60-
}
61-
62-
func initErrors() {
63-
e := json.Unmarshal([]byte(errorMapping), &emap)
64-
if e != nil {
65-
panic(e)
66-
}
6748
}
6849

6950
type callstack []uintptr
@@ -77,6 +58,7 @@ type hlError struct {
7758
stack callstack
7859
componentcode ComponentCode
7960
reasoncode ReasonCode
61+
message string
8062
args []interface{}
8163
stackGetter func(callstack) string
8264
}
@@ -90,8 +72,9 @@ func newHLError(debug bool) *hlError {
9072
}
9173

9274
func setupHLError(e *hlError, debug bool) {
93-
e.componentcode = Utility
94-
e.reasoncode = UtilityUnknownError
75+
e.componentcode = "Utility"
76+
e.reasoncode = "UnknownError"
77+
e.message = "An unknown error has occurred."
9578
if !debug {
9679
e.stackGetter = noopGetStack
9780
return
@@ -113,12 +96,12 @@ func (h *hlError) GetStack() string {
11396
return h.stackGetter(h.stack)
11497
}
11598

116-
// GetComponentCode returns the Return code
99+
// GetComponentCode returns the component name
117100
func (h *hlError) GetComponentCode() ComponentCode {
118101
return h.componentcode
119102
}
120103

121-
// GetReasonCode returns the Reason code
104+
// GetReasonCode returns the reason code - i.e. why the error occurred
122105
func (h *hlError) GetReasonCode() ReasonCode {
123106
return h.reasoncode
124107
}
@@ -130,43 +113,45 @@ func (h *hlError) GetErrorCode() string {
130113

131114
// Message returns the corresponding error message for this error in default
132115
// language.
133-
// TODO - figure out the best way to read in system language instead of using
134-
// hard-coded default language
135116
func (h *hlError) Message() string {
136117
// initialize logging level for errors from core.yaml. it can also be set
137118
// for code running on the peer dynamically via CLI using
138119
// "peer logging setlevel error <log-level>"
139120
errorLogLevelString, _ := flogging.GetModuleLevel("error")
121+
122+
message := fmt.Sprintf(h.message, h.args...)
140123
if errorLogLevelString == logging.DEBUG.String() {
141-
messageWithCallStack := fmt.Sprintf(emap[fmt.Sprintf("%s", h.componentcode)][fmt.Sprintf("%s", h.reasoncode)][language], h.args...) + "\n" + h.GetStack()
142-
return messageWithCallStack
124+
message = appendCallStack(message, h.GetStack())
143125
}
144-
return fmt.Sprintf(emap[fmt.Sprintf("%s", h.componentcode)][fmt.Sprintf("%s", h.reasoncode)][language], h.args...)
126+
127+
return message
145128
}
146129

147-
// MessageIn returns the corresponding error message for this error in 'language'
148-
func (h *hlError) MessageIn(language string) string {
149-
return fmt.Sprintf(emap[fmt.Sprintf("%s", h.componentcode)][fmt.Sprintf("%s", h.reasoncode)][language], h.args...)
130+
func appendCallStack(message string, callstack string) string {
131+
messageWithCallStack := message + "\n" + callstack
132+
133+
return messageWithCallStack
150134
}
151135

152136
// Error creates a CallStackError using a specific Component Code and
153137
// Reason Code (no callstack is recorded)
154-
func Error(componentcode ComponentCode, reasoncode ReasonCode, args ...interface{}) CallStackError {
155-
return newCustomError(componentcode, reasoncode, false, args...)
138+
func Error(componentcode ComponentCode, reasoncode ReasonCode, message string, args ...interface{}) CallStackError {
139+
return newCustomError(componentcode, reasoncode, message, false, args...)
156140
}
157141

158142
// ErrorWithCallstack creates a CallStackError using a specific Component Code and
159143
// Reason Code and fills its callstack
160-
func ErrorWithCallstack(componentcode ComponentCode, reasoncode ReasonCode, args ...interface{}) CallStackError {
161-
return newCustomError(componentcode, reasoncode, true, args...)
144+
func ErrorWithCallstack(componentcode ComponentCode, reasoncode ReasonCode, message string, args ...interface{}) CallStackError {
145+
return newCustomError(componentcode, reasoncode, message, true, args...)
162146
}
163147

164-
func newCustomError(componentcode ComponentCode, reasoncode ReasonCode, generateStack bool, args ...interface{}) CallStackError {
148+
func newCustomError(componentcode ComponentCode, reasoncode ReasonCode, message string, generateStack bool, args ...interface{}) CallStackError {
165149
e := &hlError{}
166150
setupHLError(e, generateStack)
167151
e.componentcode = componentcode
168152
e.reasoncode = reasoncode
169153
e.args = args
154+
e.message = message
170155
return e
171156
}
172157

core/errors/errors_json.go

-86
This file was deleted.

core/errors/errors_test.go

+17-23
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
func TestError(t *testing.T) {
28-
e := Error(Utility, UtilityUnknownError)
28+
e := Error("Utility", "ErrorWithArg", "An unknown error occurred.")
2929
s := e.GetStack()
3030
if s != "" {
3131
t.Fatalf("No error stack should have been recorded.")
@@ -34,15 +34,15 @@ func TestError(t *testing.T) {
3434

3535
// TestErrorWithArg tests creating an error with a message argument
3636
func TestErrorWithArg(t *testing.T) {
37-
e := Error(Utility, UtilityErrorWithArg, "arg1")
37+
e := Error("Utility", "ErrorWithArg", "An error occurred: %s", "arg1")
3838
s := e.GetStack()
3939
if s != "" {
4040
t.Fatalf("No error stack should have been recorded.")
4141
}
4242
}
4343

4444
func TestErrorWithCallstack(t *testing.T) {
45-
e := ErrorWithCallstack(Utility, UtilityUnknownError)
45+
e := ErrorWithCallstack("Utility", "UnknownError", "An unknown error occurred.")
4646
s := e.GetStack()
4747
if s == "" {
4848
t.Fatalf("No error stack was recorded.")
@@ -52,7 +52,7 @@ func TestErrorWithCallstack(t *testing.T) {
5252
// TestErrorWithCallstackAndArg tests creating an error with a callstack and
5353
// message argument
5454
func TestErrorWithCallstackAndArg(t *testing.T) {
55-
e := ErrorWithCallstack(Utility, UtilityErrorWithArg, "arg1")
55+
e := ErrorWithCallstack("Utility", "ErrorWithArg", "An error occurred: %s", "arg1")
5656
s := e.GetStack()
5757
if s == "" {
5858
t.Fatalf("No error stack was recorded.")
@@ -67,7 +67,7 @@ func TestErrorWithCallstackMessage(t *testing.T) {
6767
// to the error message
6868
flogging.SetModuleLevel("error", "debug")
6969

70-
e := ErrorWithCallstack(Utility, UtilityUnknownError)
70+
e := ErrorWithCallstack("Utility", "ErrorWithArg", "An unknown error occurred.")
7171
s := e.GetStack()
7272
if s == "" {
7373
t.Fatalf("No error stack was recorded.")
@@ -85,73 +85,67 @@ func ExampleError() {
8585
// not be appended to the error message
8686
flogging.SetModuleLevel("error", "warning")
8787

88-
err := ErrorWithCallstack(Utility, UtilityUnknownError)
88+
err := ErrorWithCallstack("Utility", "UnknownError", "An unknown error occurred.")
8989

9090
if err != nil {
9191
fmt.Printf("%s\n", err.Error())
9292
fmt.Printf("%s\n", err.GetErrorCode())
9393
fmt.Printf("%s\n", err.GetComponentCode())
9494
fmt.Printf("%s\n", err.GetReasonCode())
9595
fmt.Printf("%s\n", err.Message())
96-
fmt.Printf("%s\n", err.MessageIn("en"))
9796
// Output:
9897
// An unknown error occurred.
99-
// Utility-UtilityUnknownError
98+
// Utility-UnknownError
10099
// Utility
101-
// UtilityUnknownError
102-
// An unknown error occurred.
100+
// UnknownError
103101
// An unknown error occurred.
104102
}
105103
}
106104

107105
// ExampleErrorWithArg tests the output for a sample error with a message
108106
// argument
109-
func ExampleUtilityErrorWithArg() {
107+
func Example_utilityErrorWithArg() {
110108
// when the 'error' module is set to anything but debug, the callstack will
111109
// not be appended to the error message
112110
flogging.SetModuleLevel("error", "warning")
113111

114-
err := ErrorWithCallstack(Utility, UtilityErrorWithArg, "arg1")
112+
err := ErrorWithCallstack("Utility", "ErrorWithArg", "An error occurred: %s", "arg1")
115113

116114
if err != nil {
117115
fmt.Printf("%s\n", err.Error())
118116
fmt.Printf("%s\n", err.GetErrorCode())
119117
fmt.Printf("%s\n", err.GetComponentCode())
120118
fmt.Printf("%s\n", err.GetReasonCode())
121119
fmt.Printf("%s\n", err.Message())
122-
fmt.Printf("%s\n", err.MessageIn("en"))
123120
// Output:
124121
// An error occurred: arg1
125-
// Utility-UtilityErrorWithArg
122+
// Utility-ErrorWithArg
126123
// Utility
127-
// UtilityErrorWithArg
128-
// An error occurred: arg1
124+
// ErrorWithArg
129125
// An error occurred: arg1
130126
}
131127
}
132128

133-
// ExampleLoggingInvalidLogLevel tests the output for a logging error where
129+
// Example_loggingInvalidLevel tests the output for a logging error where
134130
// and an invalid log level has been provided
135-
func ExampleLoggingInvalidLogLevel() {
131+
func Example_loggingInvalidLevel() {
136132
// when the 'error' module is set to anything but debug, the callstack will
137133
// not be appended to the error message
138134
flogging.SetModuleLevel("error", "warning")
139135

140-
err := ErrorWithCallstack(Logging, LoggingInvalidLogLevel, "invalid")
136+
err := ErrorWithCallstack("Logging", "InvalidLevel", "Invalid log level provided - %s", "invalid")
141137

142138
if err != nil {
143139
fmt.Printf("%s\n", err.Error())
144140
fmt.Printf("%s\n", err.GetErrorCode())
145141
fmt.Printf("%s\n", err.GetComponentCode())
146142
fmt.Printf("%s\n", err.GetReasonCode())
147143
fmt.Printf("%s\n", err.Message())
148-
fmt.Printf("%s\n", err.MessageIn("en"))
149144
// Output:
150145
// Invalid log level provided - invalid
151-
// Logging-LoggingInvalidLogLevel
146+
// Logging-InvalidLevel
152147
// Logging
153-
// LoggingInvalidLogLevel
154-
// Invalid log level provided - invalid
148+
// InvalidLevel
155149
// Invalid log level provided - invalid
156150
}
157151
}

peer/clilogging/common.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ func checkLoggingCmdParams(cmd *cobra.Command, args []string) error {
2828

2929
// check that at least one parameter is passed in
3030
if len(args) == 0 {
31-
err = errors.ErrorWithCallstack(errors.Logging, errors.LoggingNoParameters)
31+
err = errors.ErrorWithCallstack("Logging", "NoParameters", "No parameters provided.")
3232
return err
3333
}
3434

3535
if cmd.Name() == "setlevel" {
3636
// check that log level parameter is provided
3737
if len(args) == 1 {
38-
err = errors.ErrorWithCallstack(errors.Logging, errors.LoggingNoLogLevelParameter)
38+
err = errors.ErrorWithCallstack("Logging", "NoLevelParameter", "No log level provided.")
3939
} else {
4040
// check that log level is valid. if not, err is set
4141
_, err = logging.LogLevel(args[1])
4242
if err != nil {
43-
err = errors.ErrorWithCallstack(errors.Logging, errors.LoggingInvalidLogLevel, args[1])
43+
err = errors.ErrorWithCallstack("Logging", "InvalidLevel", "Invalid log level provided - %s", args[1])
4444
}
4545
}
4646
}

peer/common/common.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func InitCrypto(mspMgrConfigDir string) error {
7979
func GetEndorserClient() (pb.EndorserClient, error) {
8080
clientConn, err := peer.NewPeerClientConnection()
8181
if err != nil {
82-
err = errors.ErrorWithCallstack(errors.Peer, errors.PeerConnectionError, err.Error())
82+
err = errors.ErrorWithCallstack("Peer", "ConnectionError", "Error trying to connect to local peer: %s", err.Error())
8383
return nil, err
8484
}
8585
endorserClient := pb.NewEndorserClient(clientConn)
@@ -90,7 +90,7 @@ func GetEndorserClient() (pb.EndorserClient, error) {
9090
func GetAdminClient() (pb.AdminClient, error) {
9191
clientConn, err := peer.NewPeerClientConnection()
9292
if err != nil {
93-
err = errors.ErrorWithCallstack(errors.Peer, errors.PeerConnectionError, err.Error())
93+
err = errors.ErrorWithCallstack("Peer", "ConnectionError", "Error trying to connect to local peer: %s", err.Error())
9494
return nil, err
9595
}
9696
adminClient := pb.NewAdminClient(clientConn)

0 commit comments

Comments
 (0)