@@ -2,6 +2,7 @@ package slogtest_test
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"testing"
6
7
7
8
"golang.org/x/xerrors"
@@ -108,6 +109,46 @@ func TestIgnoreErrorIs_Explicit(t *testing.T) {
108
109
l .Fatal (bg , "hello" , slog .Error (xerrors .Errorf ("test %w:" , ignored )))
109
110
}
110
111
112
+ func TestIgnoreErrorFn (t * testing.T ) {
113
+ t .Parallel ()
114
+
115
+ tb := & fakeTB {}
116
+ ignored := testCodedError {code : 777 }
117
+ notIgnored := testCodedError {code : 911 }
118
+ l := slogtest .Make (tb , & slogtest.Options {IgnoreErrorFn : func (ent slog.SinkEntry ) bool {
119
+ err , ok := slogtest .FindFirstError (ent )
120
+ if ! ok {
121
+ t .Error ("did not contain an error" )
122
+ return false
123
+ }
124
+ ce := testCodedError {}
125
+ if ! xerrors .As (err , & ce ) {
126
+ return false
127
+ }
128
+ return ce .code != 911
129
+ }})
130
+
131
+ l .Error (bg , "ignored" , slog .Error (xerrors .Errorf ("test %w:" , ignored )))
132
+ assert .Equal (t , "errors" , 0 , tb .errors )
133
+
134
+ l .Error (bg , "not ignored" , slog .Error (xerrors .Errorf ("test %w:" , notIgnored )))
135
+ assert .Equal (t , "errors" , 1 , tb .errors )
136
+
137
+ // still ignored by default for IgnoredErrorIs
138
+ l .Error (bg , "canceled" , slog .Error (xerrors .Errorf ("test %w:" , context .Canceled )))
139
+ assert .Equal (t , "errors" , 1 , tb .errors )
140
+
141
+ l .Error (bg , "new" , slog .Error (xerrors .New ("test" )))
142
+ assert .Equal (t , "errors" , 2 , tb .errors )
143
+
144
+ defer func () {
145
+ recover ()
146
+ assert .Equal (t , "fatals" , 1 , tb .fatals )
147
+ }()
148
+
149
+ l .Fatal (bg , "hello" , slog .Error (xerrors .Errorf ("test %w:" , ignored )))
150
+ }
151
+
111
152
func TestCleanup (t * testing.T ) {
112
153
t .Parallel ()
113
154
@@ -163,3 +204,11 @@ func (tb *fakeTB) Fatal(v ...interface{}) {
163
204
func (tb * fakeTB ) Cleanup (fn func ()) {
164
205
tb .cleanups = append (tb .cleanups , fn )
165
206
}
207
+
208
+ type testCodedError struct {
209
+ code int
210
+ }
211
+
212
+ func (e testCodedError ) Error () string {
213
+ return fmt .Sprintf ("code: %d" , e .code )
214
+ }
0 commit comments