Skip to content

Commit

Permalink
Exposed error/failure conversion to internalbindings (#332)
Browse files Browse the repository at this point in the history
Co-authored-by: Spencer Judge <spencer@temporal.io>
  • Loading branch information
mfateev and Sushisource authored Jan 18, 2021
1 parent 8d58517 commit 6ed1ad3
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 95 deletions.
24 changes: 12 additions & 12 deletions internal/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,8 @@ func getErrType(err error) string {
return t.Name()
}

// convertErrorToFailure converts error to failure.
func convertErrorToFailure(err error, dc converter.DataConverter) *failurepb.Failure {
// ConvertErrorToFailure converts error to failure.
func ConvertErrorToFailure(err error, dc converter.DataConverter) *failurepb.Failure {
if err == nil {
return nil
}
Expand Down Expand Up @@ -815,13 +815,13 @@ func convertErrorToFailure(err error, dc converter.DataConverter) *failurepb.Fai
failure.FailureInfo = &failurepb.Failure_ApplicationFailureInfo{ApplicationFailureInfo: failureInfo}
}

failure.Cause = convertErrorToFailure(errors.Unwrap(err), dc)
failure.Cause = ConvertErrorToFailure(errors.Unwrap(err), dc)

return failure
}

// convertFailureToError converts failure to error.
func convertFailureToError(failure *failurepb.Failure, dc converter.DataConverter) error {
// ConvertFailureToError converts failure to error.
func ConvertFailureToError(failure *failurepb.Failure, dc converter.DataConverter) error {
if failure == nil {
return nil
}
Expand All @@ -839,7 +839,7 @@ func convertFailureToError(failure *failurepb.Failure, dc converter.DataConverte
failure.GetMessage(),
applicationFailureInfo.GetType(),
applicationFailureInfo.GetNonRetryable(),
convertFailureToError(failure.GetCause(), dc),
ConvertFailureToError(failure.GetCause(), dc),
details)
}
} else if failure.GetCanceledFailureInfo() != nil {
Expand All @@ -851,14 +851,14 @@ func convertFailureToError(failure *failurepb.Failure, dc converter.DataConverte
err = NewTimeoutError(
failure.GetMessage(),
timeoutFailureInfo.GetTimeoutType(),
convertFailureToError(failure.GetCause(), dc),
ConvertFailureToError(failure.GetCause(), dc),
lastHeartbeatDetails)
} else if failure.GetTerminatedFailureInfo() != nil {
err = newTerminatedError()
} else if failure.GetServerFailureInfo() != nil {
err = NewServerError(failure.GetMessage(), failure.GetServerFailureInfo().GetNonRetryable(), convertFailureToError(failure.GetCause(), dc))
err = NewServerError(failure.GetMessage(), failure.GetServerFailureInfo().GetNonRetryable(), ConvertFailureToError(failure.GetCause(), dc))
} else if failure.GetResetWorkflowFailureInfo() != nil {
err = NewApplicationError(failure.GetMessage(), "", true, convertFailureToError(failure.GetCause(), dc), failure.GetResetWorkflowFailureInfo().GetLastHeartbeatDetails())
err = NewApplicationError(failure.GetMessage(), "", true, ConvertFailureToError(failure.GetCause(), dc), failure.GetResetWorkflowFailureInfo().GetLastHeartbeatDetails())
} else if failure.GetActivityFailureInfo() != nil {
activityTaskInfoFailure := failure.GetActivityFailureInfo()
err = NewActivityError(
Expand All @@ -868,7 +868,7 @@ func convertFailureToError(failure *failurepb.Failure, dc converter.DataConverte
activityTaskInfoFailure.GetActivityType(),
activityTaskInfoFailure.GetActivityId(),
activityTaskInfoFailure.GetRetryState(),
convertFailureToError(failure.GetCause(), dc),
ConvertFailureToError(failure.GetCause(), dc),
)
} else if failure.GetChildWorkflowExecutionFailureInfo() != nil {
childWorkflowExecutionFailureInfo := failure.GetChildWorkflowExecutionFailureInfo()
Expand All @@ -880,13 +880,13 @@ func convertFailureToError(failure *failurepb.Failure, dc converter.DataConverte
childWorkflowExecutionFailureInfo.GetInitiatedEventId(),
childWorkflowExecutionFailureInfo.GetStartedEventId(),
childWorkflowExecutionFailureInfo.GetRetryState(),
convertFailureToError(failure.GetCause(), dc),
ConvertFailureToError(failure.GetCause(), dc),
)
}

if err == nil {
// All unknown types are considered to be retryable ApplicationError.
err = NewApplicationError(failure.GetMessage(), "", false, convertFailureToError(failure.GetCause(), dc))
err = NewApplicationError(failure.GetMessage(), "", false, ConvertFailureToError(failure.GetCause(), dc))
}

if fh, ok := err.(failureHolder); ok {
Expand Down
68 changes: 34 additions & 34 deletions internal/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func Test_convertErrorToFailure_ApplicationError(t *testing.T) {
require := require.New(t)

err := NewApplicationError("message", "customType", true, errors.New("cause error"), "details", 2208)
f := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("message", f.GetMessage())
require.Equal("customType", f.GetApplicationFailureInfo().GetType())
require.Equal(true, f.GetApplicationFailureInfo().GetNonRetryable())
Expand All @@ -589,7 +589,7 @@ func Test_convertErrorToFailure_ApplicationError(t *testing.T) {
require.Equal("", f.GetCause().GetApplicationFailureInfo().GetType())
require.Nil(f.GetCause().GetCause())

err2 := convertFailureToError(f, converter.GetDefaultDataConverter())
err2 := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var applicationErr *ApplicationError
require.True(errors.As(err2, &applicationErr))
require.Equal("message (type: customType, retryable: false): cause error", applicationErr.Error())
Expand All @@ -603,13 +603,13 @@ func Test_convertErrorToFailure_CanceledError(t *testing.T) {
require := require.New(t)

err := NewCanceledError("details", 2208)
f := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("canceled", f.GetMessage())
require.Equal([]byte(`"details"`), f.GetCanceledFailureInfo().GetDetails().GetPayloads()[0].GetData())
require.Equal([]byte(`2208`), f.GetCanceledFailureInfo().GetDetails().GetPayloads()[1].GetData())
require.Nil(f.GetCause())

err2 := convertFailureToError(f, converter.GetDefaultDataConverter())
err2 := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var canceledErr *CanceledError
require.True(errors.As(err2, &canceledErr))
}
Expand All @@ -618,27 +618,27 @@ func Test_convertErrorToFailure_PanicError(t *testing.T) {
require := require.New(t)

err := newPanicError("panic message", "long call stack")
f := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("panic message", f.GetMessage())
require.Equal("PanicError", f.GetApplicationFailureInfo().GetType())
require.Equal(false, f.GetApplicationFailureInfo().GetNonRetryable())
require.Equal("long call stack", f.GetStackTrace())
require.Nil(f.GetCause())

err2 := convertFailureToError(f, converter.GetDefaultDataConverter())
err2 := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var panicErr *PanicError
require.True(errors.As(err2, &panicErr))
require.Equal(err.Error(), panicErr.Error())
require.Equal(err.StackTrace(), panicErr.StackTrace())

f = convertErrorToFailure(newWorkflowPanicError("panic message", "long call stack"), converter.GetDefaultDataConverter())
f = ConvertErrorToFailure(newWorkflowPanicError("panic message", "long call stack"), converter.GetDefaultDataConverter())
require.Equal("panic message", f.GetMessage())
require.Equal("PanicError", f.GetApplicationFailureInfo().GetType())
require.Equal(true, f.GetApplicationFailureInfo().GetNonRetryable())
require.Equal("long call stack", f.GetStackTrace())
require.Nil(f.GetCause())

err2 = convertFailureToError(f, converter.GetDefaultDataConverter())
err2 = ConvertFailureToError(f, converter.GetDefaultDataConverter())
require.True(errors.As(err2, &panicErr))
require.Equal(err.Error(), panicErr.Error())
require.Equal(err.StackTrace(), panicErr.StackTrace())
Expand All @@ -648,13 +648,13 @@ func Test_convertErrorToFailure_TimeoutError(t *testing.T) {
require := require.New(t)

err := NewTimeoutError("timeout", enumspb.TIMEOUT_TYPE_HEARTBEAT, &coolError{})
f := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("timeout", f.GetMessage())
require.Equal(enumspb.TIMEOUT_TYPE_HEARTBEAT, f.GetTimeoutFailureInfo().GetTimeoutType())
require.Equal(convertErrorToFailure(&coolError{}, converter.GetDefaultDataConverter()), f.GetCause())
require.Equal(f.GetCause(), convertErrorToFailure(&coolError{}, converter.GetDefaultDataConverter()))
require.Equal(ConvertErrorToFailure(&coolError{}, converter.GetDefaultDataConverter()), f.GetCause())
require.Equal(f.GetCause(), ConvertErrorToFailure(&coolError{}, converter.GetDefaultDataConverter()))

err2 := convertFailureToError(f, converter.GetDefaultDataConverter())
err2 := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var timeoutErr *TimeoutError
require.True(errors.As(err2, &timeoutErr))
require.Equal("timeout (type: Heartbeat): cool error (type: coolError, retryable: true)", timeoutErr.Error())
Expand All @@ -665,11 +665,11 @@ func Test_convertErrorToFailure_TerminateError(t *testing.T) {
require := require.New(t)

err := newTerminatedError()
f := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("terminated", f.GetMessage())
require.Nil(f.GetCause())

err2 := convertFailureToError(f, converter.GetDefaultDataConverter())
err2 := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var terminateErr *TerminatedError
require.True(errors.As(err2, &terminateErr))
}
Expand All @@ -678,12 +678,12 @@ func Test_convertErrorToFailure_ServerError(t *testing.T) {
require := require.New(t)

err := NewServerError("message", true, &coolError{})
f := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("message", f.GetMessage())
require.Equal(true, f.GetServerFailureInfo().GetNonRetryable())
require.Equal(convertErrorToFailure(&coolError{}, converter.GetDefaultDataConverter()), f.GetCause())
require.Equal(ConvertErrorToFailure(&coolError{}, converter.GetDefaultDataConverter()), f.GetCause())

err2 := convertFailureToError(f, converter.GetDefaultDataConverter())
err2 := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var serverErr *ServerError
require.True(errors.As(err2, &serverErr))
require.Equal("message: cool error (type: coolError, retryable: true)", serverErr.Error())
Expand All @@ -695,17 +695,17 @@ func Test_convertErrorToFailure_ActivityError(t *testing.T) {

applicationErr := NewApplicationError("app err", "", true, nil)
err := NewActivityError(8, 22, "alex", &commonpb.ActivityType{Name: "activityType"}, "32283", enumspb.RETRY_STATE_NON_RETRYABLE_FAILURE, applicationErr)
f := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("activity error", f.GetMessage())
require.Equal(int64(8), f.GetActivityFailureInfo().GetScheduledEventId())
require.Equal(int64(22), f.GetActivityFailureInfo().GetStartedEventId())
require.Equal("alex", f.GetActivityFailureInfo().GetIdentity())
require.Equal("activityType", f.GetActivityFailureInfo().GetActivityType().GetName())
require.Equal("32283", f.GetActivityFailureInfo().GetActivityId())
require.Equal(enumspb.RETRY_STATE_NON_RETRYABLE_FAILURE, f.GetActivityFailureInfo().GetRetryState())
require.Equal(convertErrorToFailure(applicationErr, converter.GetDefaultDataConverter()), f.GetCause())
require.Equal(ConvertErrorToFailure(applicationErr, converter.GetDefaultDataConverter()), f.GetCause())

err2 := convertFailureToError(f, converter.GetDefaultDataConverter())
err2 := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var activityTaskErr *ActivityError
require.True(errors.As(err2, &activityTaskErr))
require.Equal(err.Error(), activityTaskErr.Error())
Expand All @@ -722,15 +722,15 @@ func Test_convertErrorToFailure_ChildWorkflowExecutionError(t *testing.T) {

applicationErr := NewApplicationError("app err", "", true, nil)
err := NewChildWorkflowExecutionError("namespace", "wID", "rID", "wfType", 8, 22, enumspb.RETRY_STATE_NON_RETRYABLE_FAILURE, applicationErr)
f := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("child workflow execution error", f.GetMessage())
require.Equal(int64(8), f.GetChildWorkflowExecutionFailureInfo().GetInitiatedEventId())
require.Equal(int64(22), f.GetChildWorkflowExecutionFailureInfo().GetStartedEventId())
require.Equal("namespace", f.GetChildWorkflowExecutionFailureInfo().GetNamespace())
require.Equal(enumspb.RETRY_STATE_NON_RETRYABLE_FAILURE, f.GetChildWorkflowExecutionFailureInfo().GetRetryState())
require.Equal(convertErrorToFailure(applicationErr, converter.GetDefaultDataConverter()), f.GetCause())
require.Equal(ConvertErrorToFailure(applicationErr, converter.GetDefaultDataConverter()), f.GetCause())

err2 := convertFailureToError(f, converter.GetDefaultDataConverter())
err2 := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var childWorkflowExecutionErr *ChildWorkflowExecutionError
require.True(errors.As(err2, &childWorkflowExecutionErr))
require.Equal(err.Error(), childWorkflowExecutionErr.Error())
Expand All @@ -740,13 +740,13 @@ func Test_convertErrorToFailure_ChildWorkflowExecutionError(t *testing.T) {
func Test_convertErrorToFailure_UnknowError(t *testing.T) {
require := require.New(t)
err := &coolError{}
f := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("cool error", f.GetMessage())
require.Equal("coolError", f.GetApplicationFailureInfo().GetType())
require.Equal(false, f.GetApplicationFailureInfo().GetNonRetryable())
require.Nil(f.GetCause())

err2 := convertFailureToError(f, converter.GetDefaultDataConverter())
err2 := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var coolErr *ApplicationError
require.True(errors.As(err2, &coolErr))
require.Equal("cool error (type: coolError, retryable: true)", coolErr.Error())
Expand All @@ -764,7 +764,7 @@ func Test_convertErrorToFailure_SavedFailure(t *testing.T) {
Type: "SomeJavaException",
NonRetryable: true,
}}}
f := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("actual message", f.GetMessage())
require.Equal("JavaSDK", f.GetSource())
require.Equal("some stack trace", f.GetStackTrace())
Expand Down Expand Up @@ -794,7 +794,7 @@ func Test_convertFailureToError_ApplicationFailure(t *testing.T) {
},
}

err = convertFailureToError(f, converter.GetDefaultDataConverter())
err = ConvertFailureToError(f, converter.GetDefaultDataConverter())
var applicationErr *ApplicationError
require.True(errors.As(err, &applicationErr))
require.Equal("message (type: MyCoolType, retryable: false): cause message (type: UnknownType, retryable: true)", applicationErr.Error())
Expand All @@ -820,7 +820,7 @@ func Test_convertFailureToError_ApplicationFailure(t *testing.T) {
}},
}

err = convertFailureToError(f, converter.GetDefaultDataConverter())
err = ConvertFailureToError(f, converter.GetDefaultDataConverter())
var panicErr *PanicError
require.True(errors.As(err, &panicErr))
require.Equal("message", panicErr.Error())
Expand All @@ -834,7 +834,7 @@ func Test_convertFailureToError_ApplicationFailure(t *testing.T) {
}},
}

err = convertFailureToError(f, converter.GetDefaultDataConverter())
err = ConvertFailureToError(f, converter.GetDefaultDataConverter())
var coolErr *ApplicationError
require.True(errors.As(err, &coolErr))
require.Equal("message (type: CoolError, retryable: true)", coolErr.Error())
Expand All @@ -854,7 +854,7 @@ func Test_convertFailureToError_CanceledFailure(t *testing.T) {
}},
}

err = convertFailureToError(f, converter.GetDefaultDataConverter())
err = ConvertFailureToError(f, converter.GetDefaultDataConverter())
var canceledErr *CanceledError
require.True(errors.As(err, &canceledErr))
var str string
Expand All @@ -874,7 +874,7 @@ func Test_convertFailureToError_TimeoutFailure(t *testing.T) {
}},
}

err := convertFailureToError(f, converter.GetDefaultDataConverter())
err := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var timeoutErr *TimeoutError
require.True(errors.As(err, &timeoutErr))
require.Equal("timeout (type: StartToClose)", timeoutErr.Error())
Expand All @@ -890,7 +890,7 @@ func Test_convertFailureToError_ServerFailure(t *testing.T) {
}},
}

err := convertFailureToError(f, converter.GetDefaultDataConverter())
err := ConvertFailureToError(f, converter.GetDefaultDataConverter())
var serverErr *ServerError
require.True(errors.As(err, &serverErr))
require.Equal("message", serverErr.Error())
Expand Down Expand Up @@ -920,7 +920,7 @@ func Test_convertFailureToError_SaveFailure(t *testing.T) {
}},
}

err := convertFailureToError(f, converter.GetDefaultDataConverter())
err := ConvertFailureToError(f, converter.GetDefaultDataConverter())

var applicationErr *ApplicationError
require.True(errors.As(err, &applicationErr))
Expand All @@ -936,7 +936,7 @@ func Test_convertFailureToError_SaveFailure(t *testing.T) {
activityErr.scheduledEventID = 22
activityErr.identity = "bob"

f2 := convertErrorToFailure(err, converter.GetDefaultDataConverter())
f2 := ConvertErrorToFailure(err, converter.GetDefaultDataConverter())
require.Equal("message", f2.GetMessage())
require.Equal("long stack trace", f2.GetStackTrace())
require.Equal("JavaSDK", f2.GetSource())
Expand Down
10 changes: 5 additions & 5 deletions internal/internal_event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ func (weh *workflowExecutionEventHandlerImpl) handleActivityTaskFailed(event *hi
&commonpb.ActivityType{Name: activity.activityType.Name},
activityID,
attributes.GetRetryState(),
convertFailureToError(attributes.GetFailure(), weh.GetDataConverter()),
ConvertFailureToError(attributes.GetFailure(), weh.GetDataConverter()),
)

activity.handle(nil, activityTaskErr)
Expand All @@ -1003,7 +1003,7 @@ func (weh *workflowExecutionEventHandlerImpl) handleActivityTaskTimedOut(event *
}

attributes := event.GetActivityTaskTimedOutEventAttributes()
timeoutError := convertFailureToError(attributes.GetFailure(), weh.GetDataConverter())
timeoutError := ConvertFailureToError(attributes.GetFailure(), weh.GetDataConverter())

activityTaskErr := NewActivityError(
attributes.GetScheduledEventId(),
Expand Down Expand Up @@ -1151,7 +1151,7 @@ func (weh *workflowExecutionEventHandlerImpl) handleLocalActivityMarker(details
if failure != nil {
lar.Attempt = lamd.Attempt
lar.Backoff = lamd.Backoff
lar.Err = convertFailureToError(failure, weh.GetDataConverter())
lar.Err = ConvertFailureToError(failure, weh.GetDataConverter())
} else {
var result *commonpb.Payloads
var ok bool
Expand Down Expand Up @@ -1200,7 +1200,7 @@ func (weh *workflowExecutionEventHandlerImpl) ProcessLocalActivityResult(lar *lo
EventType: enumspb.EVENT_TYPE_MARKER_RECORDED,
Attributes: &historypb.HistoryEvent_MarkerRecordedEventAttributes{MarkerRecordedEventAttributes: &historypb.MarkerRecordedEventAttributes{
MarkerName: localActivityMarkerName,
Failure: convertErrorToFailure(lar.err, weh.GetDataConverter()),
Failure: ConvertErrorToFailure(lar.err, weh.GetDataConverter()),
Details: details,
}},
}
Expand Down Expand Up @@ -1290,7 +1290,7 @@ func (weh *workflowExecutionEventHandlerImpl) handleChildWorkflowExecutionFailed
attributes.GetInitiatedEventId(),
attributes.GetStartedEventId(),
attributes.GetRetryState(),
convertFailureToError(attributes.GetFailure(), weh.GetDataConverter()),
ConvertFailureToError(attributes.GetFailure(), weh.GetDataConverter()),
)
childWorkflow.handle(nil, childWorkflowExecutionError)
return nil
Expand Down
Loading

0 comments on commit 6ed1ad3

Please sign in to comment.