Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 23c0a3c

Browse files
committed
Do recover thing for SetArg
1 parent 0f4f173 commit 23c0a3c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

gomock/call.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ func (c *Call) Do(f interface{}) *Call {
162162
if !ok {
163163
panic(r)
164164
}
165-
if !strings.Contains(errMsg, "reflect: Call using") {
165+
if !strings.Contains(errMsg, "reflect: Call using") &&
166+
!strings.Contains(errMsg, "reflect.Set: value of") {
166167
panic(r)
167168
}
168169
skipFrames := 8
@@ -277,6 +278,25 @@ func (c *Call) SetArg(n int, value interface{}) *Call {
277278
case reflect.Slice:
278279
setSlice(args[n], v)
279280
default:
281+
defer func() {
282+
if r := recover(); r != nil {
283+
errMsg, ok := r.(string)
284+
285+
// We only handle a very specific panic
286+
// If it's not that one, then we "rethrow" the panic
287+
// This allows users to use functions that panic in their tests
288+
if !ok {
289+
panic(r)
290+
}
291+
if !strings.Contains(errMsg, "reflect: Call using") &&
292+
!strings.Contains(errMsg, "reflect.Set: value of") {
293+
panic(r)
294+
}
295+
skipFrames := 8
296+
stackTraceStr := "\n\n" + currentStackTrace(skipFrames)
297+
c.t.Fatalf("%s%+v", errMsg, stackTraceStr)
298+
}
299+
}()
280300
reflect.ValueOf(args[n]).Elem().Set(v)
281301
}
282302
return nil

0 commit comments

Comments
 (0)