@@ -19,10 +19,12 @@ package errors
19
19
import (
20
20
"fmt"
21
21
"testing"
22
+
23
+ "github.com/hyperledger/fabric/flogging"
22
24
)
23
25
24
26
func TestError (t * testing.T ) {
25
- e := Error (Utility , UnknownError )
27
+ e := Error (Utility , UtilityUnknownError )
26
28
s := e .GetStack ()
27
29
if s != "" {
28
30
t .Fatalf ("No error stack should have been recorded." )
@@ -31,15 +33,15 @@ func TestError(t *testing.T) {
31
33
32
34
// TestErrorWithArg tests creating an error with a message argument
33
35
func TestErrorWithArg (t * testing.T ) {
34
- e := Error (Utility , ErrorWithArg , "arg1" )
36
+ e := Error (Utility , UtilityErrorWithArg , "arg1" )
35
37
s := e .GetStack ()
36
38
if s != "" {
37
39
t .Fatalf ("No error stack should have been recorded." )
38
40
}
39
41
}
40
42
41
43
func TestErrorWithCallstack (t * testing.T ) {
42
- e := ErrorWithCallstack (Utility , UnknownError )
44
+ e := ErrorWithCallstack (Utility , UtilityUnknownError )
43
45
s := e .GetStack ()
44
46
if s == "" {
45
47
t .Fatalf ("No error stack was recorded." )
@@ -49,54 +51,126 @@ func TestErrorWithCallstack(t *testing.T) {
49
51
// TestErrorWithCallstackAndArg tests creating an error with a callstack and
50
52
// message argument
51
53
func TestErrorWithCallstackAndArg (t * testing.T ) {
52
- e := ErrorWithCallstack (Utility , ErrorWithArg , "arg1" )
54
+ e := ErrorWithCallstack (Utility , UtilityErrorWithArg , "arg1" )
53
55
s := e .GetStack ()
54
56
if s == "" {
55
57
t .Fatalf ("No error stack was recorded." )
56
58
}
57
59
}
58
60
59
- func oops () CallStackError {
60
- return Error (Utility , UnknownError )
61
- }
62
-
63
61
func ExampleError () {
64
- err := oops ()
62
+ // when the 'error' module is set to anything but debug, the callstack will
63
+ // not be appended to the error message
64
+ flogging .SetModuleLogLevel ("error" , "warning" )
65
+
66
+ err := ErrorWithCallstack (Utility , UtilityUnknownError )
67
+
65
68
if err != nil {
66
69
fmt .Printf ("%s\n " , err .Error ())
67
70
fmt .Printf ("%s\n " , err .GetErrorCode ())
68
- fmt .Printf ("%d \n " , err .GetComponentCode ())
69
- fmt .Printf ("%d \n " , err .GetReasonCode ())
71
+ fmt .Printf ("%s \n " , err .GetComponentCode ())
72
+ fmt .Printf ("%s \n " , err .GetReasonCode ())
70
73
fmt .Printf ("%s\n " , err .Message ())
71
74
fmt .Printf ("%s\n " , err .MessageIn ("en" ))
72
75
// Output:
73
76
// An unknown error occurred.
74
- // 0-0
75
- // 0
76
- // 0
77
+ // Utility-UtilityUnknownError
78
+ // Utility
79
+ // UtilityUnknownError
77
80
// An unknown error occurred.
78
81
// An unknown error occurred.
79
82
}
80
83
}
81
84
82
85
// ExampleErrorWithArg tests the output for a sample error with a message
83
86
// argument
84
- func ExampleErrorWithArg () {
85
- err := Error (Utility , ErrorWithArg , "arg1" )
87
+ func ExampleUtilityErrorWithArg () {
88
+ // when the 'error' module is set to anything but debug, the callstack will
89
+ // not be appended to the error message
90
+ flogging .SetModuleLogLevel ("error" , "warning" )
91
+
92
+ err := ErrorWithCallstack (Utility , UtilityErrorWithArg , "arg1" )
86
93
87
94
if err != nil {
88
95
fmt .Printf ("%s\n " , err .Error ())
89
96
fmt .Printf ("%s\n " , err .GetErrorCode ())
90
- fmt .Printf ("%d \n " , err .GetComponentCode ())
91
- fmt .Printf ("%d \n " , err .GetReasonCode ())
97
+ fmt .Printf ("%s \n " , err .GetComponentCode ())
98
+ fmt .Printf ("%s \n " , err .GetReasonCode ())
92
99
fmt .Printf ("%s\n " , err .Message ())
93
100
fmt .Printf ("%s\n " , err .MessageIn ("en" ))
94
101
// Output:
95
102
// An error occurred: arg1
96
- // 0-1
97
- // 0
98
- // 1
103
+ // Utility-UtilityErrorWithArg
104
+ // Utility
105
+ // UtilityErrorWithArg
99
106
// An error occurred: arg1
100
107
// An error occurred: arg1
101
108
}
102
109
}
110
+
111
+ // ExampleLoggingInvalidLogLevel tests the output for a logging error where
112
+ // and an invalid log level has been provided
113
+ func ExampleLoggingInvalidLogLevel () {
114
+ // when the 'error' module is set to anything but debug, the callstack will
115
+ // not be appended to the error message
116
+ flogging .SetModuleLogLevel ("error" , "warning" )
117
+
118
+ err := ErrorWithCallstack (Logging , LoggingInvalidLogLevel , "invalid" )
119
+
120
+ if err != nil {
121
+ fmt .Printf ("%s\n " , err .Error ())
122
+ fmt .Printf ("%s\n " , err .GetErrorCode ())
123
+ fmt .Printf ("%s\n " , err .GetComponentCode ())
124
+ fmt .Printf ("%s\n " , err .GetReasonCode ())
125
+ fmt .Printf ("%s\n " , err .Message ())
126
+ fmt .Printf ("%s\n " , err .MessageIn ("en" ))
127
+ // Output:
128
+ // Invalid log level provided - invalid
129
+ // Logging-LoggingInvalidLogLevel
130
+ // Logging
131
+ // LoggingInvalidLogLevel
132
+ // Invalid log level provided - invalid
133
+ // Invalid log level provided - invalid
134
+ }
135
+ }
136
+
137
+ // ExampleLoggingInvalidLogLevel tests the output for a logging error where
138
+ // and an invalid log level has been provided and the stack trace should be
139
+ // displayed with the error message
140
+ func ExampleLoggingInvalidLogLevel_withCallstack () {
141
+ // when the 'error' module is set to debug, the callstack will be appended
142
+ // to the error message
143
+ flogging .SetModuleLogLevel ("error" , "debug" )
144
+
145
+ err := ErrorWithCallstack (Logging , LoggingInvalidLogLevel , "invalid" )
146
+
147
+ if err != nil {
148
+ fmt .Printf ("%s" , err .Error ())
149
+ fmt .Printf ("%s\n " , err .GetErrorCode ())
150
+ fmt .Printf ("%s\n " , err .GetComponentCode ())
151
+ fmt .Printf ("%s\n " , err .GetReasonCode ())
152
+ fmt .Printf ("%s" , err .Message ())
153
+ fmt .Printf ("%s\n " , err .MessageIn ("en" ))
154
+ // Output:
155
+ // Invalid log level provided - invalid
156
+ // /opt/gopath/src/github.com/hyperledger/fabric/core/errors/errors_test.go:145 github.com/hyperledger/fabric/core/errors.ExampleLoggingInvalidLogLevel_withCallstack
157
+ // /opt/go/src/testing/example.go:115 testing.runExample
158
+ // /opt/go/src/testing/example.go:38 testing.RunExamples
159
+ // /opt/go/src/testing/testing.go:744 testing.(*M).Run
160
+ // github.com/hyperledger/fabric/core/errors/_test/_testmain.go:116 main.main
161
+ // /opt/go/src/runtime/proc.go:192 runtime.main
162
+ // /opt/go/src/runtime/asm_amd64.s:2087 runtime.goexit
163
+ // Logging-LoggingInvalidLogLevel
164
+ // Logging
165
+ // LoggingInvalidLogLevel
166
+ // Invalid log level provided - invalid
167
+ // /opt/gopath/src/github.com/hyperledger/fabric/core/errors/errors_test.go:145 github.com/hyperledger/fabric/core/errors.ExampleLoggingInvalidLogLevel_withCallstack
168
+ // /opt/go/src/testing/example.go:115 testing.runExample
169
+ // /opt/go/src/testing/example.go:38 testing.RunExamples
170
+ // /opt/go/src/testing/testing.go:744 testing.(*M).Run
171
+ // github.com/hyperledger/fabric/core/errors/_test/_testmain.go:116 main.main
172
+ // /opt/go/src/runtime/proc.go:192 runtime.main
173
+ // /opt/go/src/runtime/asm_amd64.s:2087 runtime.goexit
174
+ // Invalid log level provided - invalid
175
+ }
176
+ }
0 commit comments