@@ -17,6 +17,7 @@ package gomock
17
17
import (
18
18
"fmt"
19
19
"reflect"
20
+ "runtime"
20
21
"strconv"
21
22
"strings"
22
23
)
@@ -149,6 +150,27 @@ func (c *Call) Do(f interface{}) *Call {
149
150
vargs [i ] = reflect .Zero (ft .In (i ))
150
151
}
151
152
}
153
+ defer func () {
154
+ if r := recover (); r != nil {
155
+ errMsg , ok := r .(string )
156
+
157
+ // We only handle a very specific panic
158
+ // If it's not that one, then we "rethrow" the panic
159
+ // This allows users to use functions that panic in their tests
160
+ if ! ok {
161
+ panic (r )
162
+ }
163
+ if ! strings .Contains (errMsg , "reflect: Call using" ) &&
164
+ ! strings .Contains (errMsg , "reflect.Set: value of" ) {
165
+ panic (r )
166
+ }
167
+ skipFrames := 8
168
+ stackTraceStr := "\n \n " + currentStackTrace (skipFrames )
169
+ funcPC := v .Pointer ()
170
+ file , line := runtime .FuncForPC (funcPC ).FileLine (funcPC )
171
+ c .t .Fatalf ("%s (incorrect func args at %s:%d?)%+v" , errMsg , file , line , stackTraceStr )
172
+ }
173
+ }()
152
174
v .Call (vargs )
153
175
return nil
154
176
})
@@ -239,6 +261,25 @@ func (c *Call) SetArg(n int, value interface{}) *Call {
239
261
case reflect .Slice :
240
262
setSlice (args [n ], v )
241
263
default :
264
+ defer func () {
265
+ if r := recover (); r != nil {
266
+ errMsg , ok := r .(string )
267
+
268
+ // We only handle a very specific panic
269
+ // If it's not that one, then we "rethrow" the panic
270
+ // This allows users to use functions that panic in their tests
271
+ if ! ok {
272
+ panic (r )
273
+ }
274
+ if ! strings .Contains (errMsg , "reflect: Call using" ) &&
275
+ ! strings .Contains (errMsg , "reflect.Set: value of" ) {
276
+ panic (r )
277
+ }
278
+ skipFrames := 8
279
+ stackTraceStr := "\n \n " + currentStackTrace (skipFrames )
280
+ c .t .Fatalf ("%s%+v" , errMsg , stackTraceStr )
281
+ }
282
+ }()
242
283
reflect .ValueOf (args [n ]).Elem ().Set (v )
243
284
}
244
285
return nil
@@ -382,7 +423,7 @@ func (c *Call) matches(args []interface{}) error {
382
423
383
424
// Check that the call is not exhausted.
384
425
if c .exhausted () {
385
- return fmt .Errorf ("Expected call at %s has already been called the max number of times." , c .origin )
426
+ return fmt .Errorf ("Expected call at %s has already been called the max number of times (%d) ." , c .origin , c . maxCalls )
386
427
}
387
428
388
429
return nil
0 commit comments