Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling to differentiate different error types #29

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions integ/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ func TestBasicWorkflow(t *testing.T) {
})
assert.Nil(t, err)
assert.NotEmpty(t, runId)

// start the same workflowId again will fail
_, err = client.StartWorkflow(context.Background(), &basicWorkflow{}, wfId, 10, nil, nil)
assert.True(t, iwf.IsWorkflowAlreadyStartedError(err))

var output int
err = client.GetSimpleWorkflowResult(context.Background(), wfId, "", &output)
assert.Nil(t, err)
assert.Equal(t, 3, output)

err = client.GetSimpleWorkflowResult(context.Background(), "a wrong workflowId", "", &output)
assert.True(t, iwf.IsWorkflowNotExistsError(err))
}
2 changes: 1 addition & 1 deletion integ/persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ func TestPersistenceWorkflow(t *testing.T) {
PageSize: iwfidl.PtrInt32(1),
NextPageToken: nil,
})
assert.Nil(t, err, iwf.GetOpenApiErrorDetailedMessage(err))
assert.Nil(t, err, iwf.GetOpenApiErrorBody(err))
assert.True(t, len(resp.WorkflowExecutions) > 0)
}
3 changes: 3 additions & 0 deletions integ/signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func TestSignalWorkflow(t *testing.T) {
err = client.GetSimpleWorkflowResult(context.Background(), wfId, "", &output)
assert.Nil(t, err)
assert.Equal(t, 100, output)

err = client.SignalWorkflow(context.Background(), &signalWorkflow{}, "a wrong workflowId", "", testChannelName1, 100)
assert.True(t, iwf.IsWorkflowNotExistsError(err))
}

func TestSignalWorkflowWithUntypedClient(t *testing.T) {
Expand Down
80 changes: 60 additions & 20 deletions iwf/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,18 @@ func NewWorkflowDefinitionErrorFmt(tpl string, arg ...interface{}) error {
}
}

type InternalServiceError struct {
Message string
Status int
HttpResponse http.Response
}

func NewInternalServiceError(message string, httpResponse http.Response) error {
return &InternalServiceError{
Message: message,
Status: httpResponse.StatusCode,
HttpResponse: httpResponse,
}
type InternalError struct {
Message string
}

func newInternalError(format string, args ...interface{}) error {
return &InternalServiceError{
return &InternalError{
Message: fmt.Sprintf(format, args...),
}
}

func (i InternalServiceError) Error() string {
return fmt.Sprintf("error message:%v, statusCode: %v", i.Message, i.Status)
func (i InternalError) Error() string {
return fmt.Sprintf("error in SDK or service: message:%v", i.Message)
}

type StateExecutionError struct {
Expand Down Expand Up @@ -117,11 +107,61 @@ func captureStateExecutionError(errPanic interface{}, retError *error) {
}
}

// GetOpenApiErrorDetailedMessage retrieve the API error body into a string to be human-readable
func GetOpenApiErrorDetailedMessage(err error) string {
oerr, ok := err.(*iwfidl.GenericOpenAPIError)
type ApiError struct {
StatusCode int
OriginalError error
OpenApiError *iwfidl.GenericOpenAPIError
HttpResponse *http.Response
Response *iwfidl.ErrorResponse
}

func (i *ApiError) Error() string {
return i.OriginalError.Error()
}

func NewApiError(originalError error, openApiError *iwfidl.GenericOpenAPIError, httpResponse *http.Response, response *iwfidl.ErrorResponse) error {
statusCode := 0
if httpResponse != nil {
statusCode = httpResponse.StatusCode
}
return &ApiError{
StatusCode: statusCode,
OriginalError: originalError,
OpenApiError: openApiError,
HttpResponse: httpResponse,
Response: response,
}
}

// GetOpenApiErrorBody retrieve the API error body into a string to be human-readable
func GetOpenApiErrorBody(err error) string {
apiError, ok := err.(*ApiError)
if !ok {
return "not an ApiError"
}
return string(apiError.OpenApiError.Body())
}

func IsClientError(err error) bool {
apiError, ok := err.(*ApiError)
if !ok {
return "not an OpenAPI Generic Error type"
return false
}
return apiError.StatusCode >= 400 && apiError.StatusCode < 500
}

func IsWorkflowAlreadyStartedError(err error) bool {
apiError, ok := err.(*ApiError)
if !ok || apiError.Response == nil {
return false
}
return apiError.Response.GetSubStatus() == iwfidl.WORKFLOW_ALREADY_STARTED_SUB_STATUS
}

func IsWorkflowNotExistsError(err error) bool {
apiError, ok := err.(*ApiError)
if !ok || apiError.Response == nil {
return false
}
return string(oerr.Body())
return apiError.Response.GetSubStatus() == iwfidl.WORKFLOW_NOT_EXISTS_SUB_STATUS
}
15 changes: 10 additions & 5 deletions iwf/unregistered_client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,16 @@ func (u *unregisteredClientImpl) doSkipTimer(ctx context.Context, workflowId, wo
}

func (u *unregisteredClientImpl) processError(err error, httpResp *http.Response) error {
if err != nil {
return err
if err == nil && httpResp != nil && httpResp.StatusCode == http.StatusOK {
return nil
}
if httpResp.StatusCode != http.StatusOK {
return NewInternalServiceError("HTTP request failed", *httpResp)
var resp *iwfidl.ErrorResponse
oerr, ok := err.(*iwfidl.GenericOpenAPIError)
if ok {
rsp, ok := oerr.Model().(iwfidl.ErrorResponse)
if ok {
resp = &rsp
}
}
return nil
return NewApiError(err, oerr, httpResp, resp)
}