Skip to content

Commit 89d2164

Browse files
committed
Add ability to pass arguments to error strings
This commit adds the ability to pass in a variable number of arguments to be used in formatting error messages that use the error handling framework defined in core/errors. Change-Id: I7747f2ee1ee5dbcaa3693b1e2e0c1205c159b02f Signed-off-by: Will Lahti <[email protected]>
1 parent 75cfb3b commit 89d2164

File tree

3 files changed

+70
-19
lines changed

3 files changed

+70
-19
lines changed

core/errors/errors.go

+21-13
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ const (
3939

4040
// Result codes
4141
const (
42-
// Placeholder
42+
// Placeholders
4343
UnknownError ReasonCode = iota
44+
ErrorWithArg ReasonCode = 1
4445
)
4546

4647
// CallStackError is a general interface for
@@ -51,6 +52,8 @@ type CallStackError interface {
5152
GetErrorCode() string
5253
GetComponentCode() ComponentCode
5354
GetReasonCode() ReasonCode
55+
Message() string
56+
MessageIn(string) string
5457
}
5558

5659
type errormap map[string]map[string]map[string]string
@@ -81,6 +84,7 @@ type hlError struct {
8184
stack callstack
8285
componentcode ComponentCode
8386
reasoncode ReasonCode
87+
args []interface{}
8488
stackGetter func(callstack) string
8589
}
8690

@@ -108,7 +112,7 @@ func setupHLError(e *hlError, debug bool) {
108112

109113
// Error comes from the error interface
110114
func (h *hlError) Error() string {
111-
return h.componentcode.Message(h.reasoncode)
115+
return h.Message()
112116
}
113117

114118
// GetStack returns the call stack as a string
@@ -131,33 +135,37 @@ func (h *hlError) GetErrorCode() string {
131135
return fmt.Sprintf("%d-%d", h.componentcode, h.reasoncode)
132136
}
133137

134-
// Message returns the corresponding error message for this code in default language
135-
func (c ComponentCode) Message(reasoncode ReasonCode) string {
136-
return emap[fmt.Sprintf("%d", c)][fmt.Sprintf("%d", reasoncode)][language]
138+
// Message returns the corresponding error message for this error in default
139+
// language.
140+
// TODO - figure out the best way to read in system language instead of using
141+
// hard-coded default language
142+
func (h *hlError) Message() string {
143+
return fmt.Sprintf(emap[fmt.Sprintf("%d", h.componentcode)][fmt.Sprintf("%d", h.reasoncode)][language], h.args...)
137144
}
138145

139-
// MessageIn returns the corresponding error message for this code in 'language'
140-
func (c ComponentCode) MessageIn(reasoncode ReasonCode, language string) string {
141-
return emap[fmt.Sprintf("%d", c)][fmt.Sprintf("%d", reasoncode)][language]
146+
// MessageIn returns the corresponding error message for this error in 'language'
147+
func (h *hlError) MessageIn(language string) string {
148+
return fmt.Sprintf(emap[fmt.Sprintf("%d", h.componentcode)][fmt.Sprintf("%d", h.reasoncode)][language], h.args...)
142149
}
143150

144151
// Error creates a CallStackError using a specific Component Code and
145152
// Reason Code (no callstack is recorded)
146-
func Error(componentcode ComponentCode, reasoncode ReasonCode) CallStackError {
147-
return newCustomError(componentcode, reasoncode, false)
153+
func Error(componentcode ComponentCode, reasoncode ReasonCode, args ...interface{}) CallStackError {
154+
return newCustomError(componentcode, reasoncode, false, args...)
148155
}
149156

150157
// ErrorWithCallstack creates a CallStackError using a specific Component Code and
151158
// Reason Code and fills its callstack
152-
func ErrorWithCallstack(componentcode ComponentCode, reasoncode ReasonCode) CallStackError {
153-
return newCustomError(componentcode, reasoncode, true)
159+
func ErrorWithCallstack(componentcode ComponentCode, reasoncode ReasonCode, args ...interface{}) CallStackError {
160+
return newCustomError(componentcode, reasoncode, true, args...)
154161
}
155162

156-
func newCustomError(componentcode ComponentCode, reasoncode ReasonCode, generateStack bool) CallStackError {
163+
func newCustomError(componentcode ComponentCode, reasoncode ReasonCode, generateStack bool, args ...interface{}) CallStackError {
157164
e := &hlError{}
158165
setupHLError(e, generateStack)
159166
e.componentcode = componentcode
160167
e.reasoncode = reasoncode
168+
e.args = args
161169
return e
162170
}
163171

core/errors/errors_json.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package errors
1919
const errorCodes string = `
2020
{"0" :
2121
{"0" :
22-
{"en": "An unknown error occured."}
22+
{"en": "An unknown error occurred."},
23+
"1":
24+
{"en": "An error occurred: %s"}
2325
}
2426
}`

core/errors/errors_test.go

+46-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ func TestError(t *testing.T) {
2929
}
3030
}
3131

32+
// TestErrorWithArg tests creating an error with a message argument
33+
func TestErrorWithArg(t *testing.T) {
34+
e := Error(Utility, ErrorWithArg, "arg1")
35+
s := e.GetStack()
36+
if s != "" {
37+
t.Fatalf("No error stack should have been recorded.")
38+
}
39+
}
40+
3241
func TestErrorWithCallstack(t *testing.T) {
3342
e := ErrorWithCallstack(Utility, UnknownError)
3443
s := e.GetStack()
@@ -37,6 +46,16 @@ func TestErrorWithCallstack(t *testing.T) {
3746
}
3847
}
3948

49+
// TestErrorWithCallstackAndArg tests creating an error with a callstack and
50+
// message argument
51+
func TestErrorWithCallstackAndArg(t *testing.T) {
52+
e := ErrorWithCallstack(Utility, ErrorWithArg, "arg1")
53+
s := e.GetStack()
54+
if s == "" {
55+
t.Fatalf("No error stack was recorded.")
56+
}
57+
}
58+
4059
func oops() CallStackError {
4160
return Error(Utility, UnknownError)
4261
}
@@ -48,14 +67,36 @@ func ExampleError() {
4867
fmt.Printf("%s\n", err.GetErrorCode())
4968
fmt.Printf("%d\n", err.GetComponentCode())
5069
fmt.Printf("%d\n", err.GetReasonCode())
51-
fmt.Printf("%s\n", err.GetComponentCode().Message(err.GetReasonCode()))
52-
fmt.Printf("%s", err.GetComponentCode().MessageIn(err.GetReasonCode(), "en"))
70+
fmt.Printf("%s\n", err.Message())
71+
fmt.Printf("%s\n", err.MessageIn("en"))
5372
// Output:
54-
// An unknown error occured.
73+
// An unknown error occurred.
5574
// 0-0
5675
// 0
5776
// 0
58-
// An unknown error occured.
59-
// An unknown error occured.
77+
// An unknown error occurred.
78+
// An unknown error occurred.
79+
}
80+
}
81+
82+
// ExampleErrorWithArg tests the output for a sample error with a message
83+
// argument
84+
func ExampleErrorWithArg() {
85+
err := Error(Utility, ErrorWithArg, "arg1")
86+
87+
if err != nil {
88+
fmt.Printf("%s\n", err.Error())
89+
fmt.Printf("%s\n", err.GetErrorCode())
90+
fmt.Printf("%d\n", err.GetComponentCode())
91+
fmt.Printf("%d\n", err.GetReasonCode())
92+
fmt.Printf("%s\n", err.Message())
93+
fmt.Printf("%s\n", err.MessageIn("en"))
94+
// Output:
95+
// An error occurred: arg1
96+
// 0-1
97+
// 0
98+
// 1
99+
// An error occurred: arg1
100+
// An error occurred: arg1
60101
}
61102
}

0 commit comments

Comments
 (0)