@@ -18,11 +18,19 @@ import (
18
18
func New () * Verify {
19
19
v := & Verify {
20
20
creationStack : captureCreationStack (),
21
+ errFactory : fmt .Errorf ,
21
22
}
22
23
runtime .SetFinalizer (v , printWarningOnUncheckedVerification )
23
24
return v
24
25
}
25
26
27
+ // WithErrFactory sets error construction function (default: fmt.Errorf).
28
+ // Use it to set custom error type of error, returned by Verify.GetError().
29
+ func (v * Verify ) WithErrFactory (factory func (string , ... interface {}) error ) * Verify {
30
+ v .errFactory = factory
31
+ return v
32
+ }
33
+
26
34
// Offensive creates verification instance (not-recommended).
27
35
// It tracks verification state and stops application process when founds unchecked verification.
28
36
// If you forget to check internal error, using `GetError` or `PanicOnError` methods,
@@ -45,6 +53,7 @@ func Offensive() *Verify {
45
53
type Verify struct {
46
54
creationStack []uintptr
47
55
err error
56
+ errFactory func (string , ... interface {}) error
48
57
checked bool
49
58
}
50
59
@@ -72,29 +81,28 @@ func (v *Verify) WithError(positiveCondition bool, err error) *Verify {
72
81
// That verifies condition passed as first argument.
73
82
// If `positiveCondition == true`, verification will proceed for other checks.
74
83
// If `positiveCondition == false`, internal state will be filled with error,
75
- // using message argument as format in fmt.Errorf (message, args...).
84
+ // using message argument as format in error factory func (message, args...) (default: fmt.Errorf ).
76
85
// After the first failed verification all others won't count and predicates won't be evaluated.
77
86
func (v * Verify ) That (positiveCondition bool , message string , args ... interface {}) * Verify {
78
87
vObj := v
79
88
if v == nil {
80
89
vObj = & Verify {}
81
90
}
82
-
83
91
vObj .checked = false
84
92
if vObj .err != nil {
85
93
return vObj
86
94
}
87
95
if positiveCondition {
88
96
return vObj
89
97
}
90
- vObj .err = fmt . Errorf (message , args ... )
98
+ vObj .err = vObj . errorf (message , args ... )
91
99
return vObj
92
100
}
93
101
94
102
// That evaluates predicate passed as first argument.
95
103
// If `predicate() == true`, verification will proceed for other checks.
96
104
// If `predicate() == false`, internal state will be filled with error,
97
- // using message argument as format in fmt.Errorf (message, args...).
105
+ // using message argument as format in error factory func (message, args...) (default: fmt.Errorf ).
98
106
// After the first failed verification all others won't count and predicates won't be evaluated.
99
107
func (v * Verify ) Predicate (predicate func () bool , message string , args ... interface {}) * Verify {
100
108
vObj := v
@@ -108,7 +116,7 @@ func (v *Verify) Predicate(predicate func() bool, message string, args ...interf
108
116
if predicate () {
109
117
return vObj
110
118
}
111
- vObj .err = fmt . Errorf (message , args ... )
119
+ vObj .err = vObj . errorf (message , args ... )
112
120
return vObj
113
121
}
114
122
@@ -144,6 +152,13 @@ func (v *Verify) String() string {
144
152
return "verification failure: " + v .err .Error ()
145
153
}
146
154
155
+ func (v * Verify ) errorf (message string , args ... interface {}) error {
156
+ if v .errFactory == nil {
157
+ v .errFactory = fmt .Errorf
158
+ }
159
+ return v .errFactory (message , args ... )
160
+ }
161
+
147
162
func (v * Verify ) printCreationStack (writer io.Writer ) {
148
163
frames := runtime .CallersFrames (v .creationStack )
149
164
for {
0 commit comments