@@ -19,10 +19,12 @@ package errors
1919import (
2020 "fmt"
2121 "testing"
22+
23+ "github.com/hyperledger/fabric/flogging"
2224)
2325
2426func TestError (t * testing.T ) {
25- e := Error (Utility , UnknownError )
27+ e := Error (Utility , UtilityUnknownError )
2628 s := e .GetStack ()
2729 if s != "" {
2830 t .Fatalf ("No error stack should have been recorded." )
@@ -31,15 +33,15 @@ func TestError(t *testing.T) {
3133
3234// TestErrorWithArg tests creating an error with a message argument
3335func TestErrorWithArg (t * testing.T ) {
34- e := Error (Utility , ErrorWithArg , "arg1" )
36+ e := Error (Utility , UtilityErrorWithArg , "arg1" )
3537 s := e .GetStack ()
3638 if s != "" {
3739 t .Fatalf ("No error stack should have been recorded." )
3840 }
3941}
4042
4143func TestErrorWithCallstack (t * testing.T ) {
42- e := ErrorWithCallstack (Utility , UnknownError )
44+ e := ErrorWithCallstack (Utility , UtilityUnknownError )
4345 s := e .GetStack ()
4446 if s == "" {
4547 t .Fatalf ("No error stack was recorded." )
@@ -49,54 +51,126 @@ func TestErrorWithCallstack(t *testing.T) {
4951// TestErrorWithCallstackAndArg tests creating an error with a callstack and
5052// message argument
5153func TestErrorWithCallstackAndArg (t * testing.T ) {
52- e := ErrorWithCallstack (Utility , ErrorWithArg , "arg1" )
54+ e := ErrorWithCallstack (Utility , UtilityErrorWithArg , "arg1" )
5355 s := e .GetStack ()
5456 if s == "" {
5557 t .Fatalf ("No error stack was recorded." )
5658 }
5759}
5860
59- func oops () CallStackError {
60- return Error (Utility , UnknownError )
61- }
62-
6361func 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+
6568 if err != nil {
6669 fmt .Printf ("%s\n " , err .Error ())
6770 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 ())
7073 fmt .Printf ("%s\n " , err .Message ())
7174 fmt .Printf ("%s\n " , err .MessageIn ("en" ))
7275 // Output:
7376 // An unknown error occurred.
74- // 0-0
75- // 0
76- // 0
77+ // Utility-UtilityUnknownError
78+ // Utility
79+ // UtilityUnknownError
7780 // An unknown error occurred.
7881 // An unknown error occurred.
7982 }
8083}
8184
8285// ExampleErrorWithArg tests the output for a sample error with a message
8386// 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" )
8693
8794 if err != nil {
8895 fmt .Printf ("%s\n " , err .Error ())
8996 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 ())
9299 fmt .Printf ("%s\n " , err .Message ())
93100 fmt .Printf ("%s\n " , err .MessageIn ("en" ))
94101 // Output:
95102 // An error occurred: arg1
96- // 0-1
97- // 0
98- // 1
103+ // Utility-UtilityErrorWithArg
104+ // Utility
105+ // UtilityErrorWithArg
99106 // An error occurred: arg1
100107 // An error occurred: arg1
101108 }
102109}
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