Skip to content

Commit f882a29

Browse files
authored
Fix decoded panic when testing CustomError (#999)
1 parent 3ac9595 commit f882a29

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

internal/activity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type (
6060
// When an activity is a function the name is an actual activity type name.
6161
// When an activity is part of a structure then each member of the structure becomes an activity with
6262
// this Name as a prefix + activity function name.
63-
Name string
63+
Name string
6464
// Activity type name is equal to function name instead of fully qualified
6565
// name including function package (and struct type if used).
6666
// This option has no effect when explicit Name is provided.

internal/error_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,19 @@ func Test_CustomError_Pointer(t *testing.T) {
285285
require.Equal(t, &testErrorDetails4, b2)
286286
}
287287

288+
func Test_CustomError_WrongDecodedType(t *testing.T) {
289+
err := NewCustomError("reason", testErrorDetails1, testErrorDetails2)
290+
var d1 string
291+
var d2 string // will cause error since it should be of type int
292+
err1 := err.Details(&d1, &d2)
293+
require.Error(t, err1)
294+
295+
err = NewCustomError("reason", testErrorDetails3)
296+
var d3 testStruct2 // will cause error since it should be of type testStruct
297+
err2 := err.Details(&d3)
298+
require.Error(t, err2)
299+
}
300+
288301
func Test_CanceledError(t *testing.T) {
289302
// test ErrorDetailValues as Details
290303
var a1 string
@@ -391,6 +404,13 @@ func TestErrorDetailsValues(t *testing.T) {
391404
require.Equal(t, ErrTooManyArg, e.Get(&a1, &a2, &a3, &a3))
392405
}
393406

407+
func TestErrorDetailsValues_WrongDecodedType(t *testing.T) {
408+
e := ErrorDetailsValues{testErrorDetails1}
409+
var d1 int // will cause error since it should be of type string
410+
err := e.Get(&d1)
411+
require.Error(t, err)
412+
}
413+
394414
func Test_SignalExternalWorkflowExecutionFailedError(t *testing.T) {
395415
context := &workflowEnvironmentImpl{
396416
decisionsHelper: newDecisionsHelper(),

internal/workflow_testsuite.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ func (b ErrorDetailsValues) Get(valuePtr ...interface{}) error {
101101
return ErrTooManyArg
102102
}
103103
for i, item := range valuePtr {
104-
reflect.ValueOf(item).Elem().Set(reflect.ValueOf(b[i]))
104+
target := reflect.ValueOf(item).Elem()
105+
val := reflect.ValueOf(b[i])
106+
if target.Type() != val.Type() {
107+
return fmt.Errorf(
108+
"unable to decode argument: cannot set %v value to %v field", val.Type(), target.Type())
109+
}
110+
target.Set(val)
105111
}
106112
return nil
107113
}

0 commit comments

Comments
 (0)