@@ -18,6 +18,7 @@ package errors
18
18
19
19
import (
20
20
"fmt"
21
+ "strings"
21
22
"testing"
22
23
23
24
"github.com/hyperledger/fabric/flogging"
@@ -58,6 +59,27 @@ func TestErrorWithCallstackAndArg(t *testing.T) {
58
59
}
59
60
}
60
61
62
+ // TestErrorWithCallstackMessage tests the output for a logging error where
63
+ // and an invalid log level has been provided and the stack trace should be
64
+ // displayed with the error message
65
+ func TestErrorWithCallstackMessage (t * testing.T ) {
66
+ // when the 'error' module is set to debug, the callstack will be appended
67
+ // to the error message
68
+ flogging .SetModuleLogLevel ("error" , "debug" )
69
+
70
+ e := ErrorWithCallstack (Utility , UtilityUnknownError )
71
+ s := e .GetStack ()
72
+ if s == "" {
73
+ t .Fatalf ("No error stack was recorded." )
74
+ }
75
+
76
+ // check that the error message contains this part of the stack trace, which
77
+ // is non-platform specific
78
+ if ! strings .Contains (e .Error (), "github.com/hyperledger/fabric/core/errors.TestErrorWithCallstackMessage" ) {
79
+ t .Fatalf ("Error message does not have stack trace appended." )
80
+ }
81
+ }
82
+
61
83
func ExampleError () {
62
84
// when the 'error' module is set to anything but debug, the callstack will
63
85
// not be appended to the error message
@@ -133,44 +155,3 @@ func ExampleLoggingInvalidLogLevel() {
133
155
// Invalid log level provided - invalid
134
156
}
135
157
}
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