Skip to content

Commit fb7f84f

Browse files
committed
Handle timeout error construct by the old client version (#852)
1 parent 3de7e33 commit fb7f84f

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

internal/internal_utils.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,15 @@ func getErrorDetails(err error, dataConverter DataConverter) (string, []byte) {
226226
// constructError construct error from reason and details sending down from server.
227227
func constructError(reason string, details []byte, dataConverter DataConverter) error {
228228
if strings.HasPrefix(reason, errReasonTimeout) {
229-
timeoutType := getTimeoutTypeFromErrReason(reason)
230229
details := newEncodedValues(details, dataConverter)
230+
timeoutType, err := getTimeoutTypeFromErrReason(reason)
231+
if err != nil {
232+
// prior client version uses details to indicate timeoutType
233+
if err := details.Get(&timeoutType); err != nil {
234+
panic(err)
235+
}
236+
return NewTimeoutError(timeoutType)
237+
}
231238
return NewTimeoutError(timeoutType, details)
232239
}
233240

@@ -286,12 +293,12 @@ func getMetricsScopeForLocalActivity(ts *metrics.TaggedScope, workflowType, loca
286293
return ts.GetTaggedScope(tagWorkflowType, workflowType, tagLocalActivityType, localActivityType)
287294
}
288295

289-
func getTimeoutTypeFromErrReason(reason string) s.TimeoutType {
296+
func getTimeoutTypeFromErrReason(reason string) (s.TimeoutType, error) {
290297
timeoutTypeStr := reason[strings.Index(reason, " ")+1:]
291298
var timeoutType s.TimeoutType
292299
if err := timeoutType.UnmarshalText([]byte(timeoutTypeStr)); err != nil {
293-
// this should never happen
294-
panic(err)
300+
// this happens when the timeout error reason is constructed by an prior constructed by prior client version
301+
return 0, err
295302
}
296-
return timeoutType
303+
return timeoutType, nil
297304
}

internal/internal_utils_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,28 @@ func TestGetErrorDetails_TimeoutError(t *testing.T) {
127127
require.Equal(t, fmt.Sprintf("%v %v", errReasonTimeout, s.TimeoutTypeHeartbeat), reason)
128128
require.Equal(t, val2, data)
129129
}
130+
131+
func TestConstructError_TimeoutError(t *testing.T) {
132+
dc := getDefaultDataConverter()
133+
details, err := dc.ToData(testErrorDetails1)
134+
require.NoError(t, err)
135+
136+
reason := fmt.Sprintf("%v %v", errReasonTimeout, s.TimeoutTypeHeartbeat)
137+
constructedErr := constructError(reason, details, dc)
138+
timeoutErr, ok := constructedErr.(*TimeoutError)
139+
require.True(t, ok)
140+
require.True(t, timeoutErr.HasDetails())
141+
var detailValue string
142+
err = timeoutErr.Details(&detailValue)
143+
require.NoError(t, err)
144+
require.Equal(t, testErrorDetails1, detailValue)
145+
146+
// Backward compatibility test
147+
reason = errReasonTimeout
148+
details, err = dc.ToData(s.TimeoutTypeHeartbeat)
149+
constructedErr = constructError(reason, details, dc)
150+
timeoutErr, ok = constructedErr.(*TimeoutError)
151+
require.True(t, ok)
152+
require.Equal(t, s.TimeoutTypeHeartbeat, timeoutErr.TimeoutType())
153+
require.False(t, timeoutErr.HasDetails())
154+
}

0 commit comments

Comments
 (0)