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

IWF-125: Limit error size return from API #429

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 15 additions & 2 deletions service/interpreter/activityImpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,29 @@ func composeHttpError(
maxL := len(errMsg)
if maxL > 5 {
maxL = 5
errMsg = errMsg[:maxL] + "..."
}
errMsg = errMsg[:maxL] + "..."

maxL = len(responseBody)
if maxL > 50 {
maxL = 50
responseBody = responseBody[:maxL] + "..."
}
responseBody = responseBody[:maxL] + "..."
errType = "1st-attempt-failure"
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe nice to create a small func for the repeated code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

maxL := len(errMsg)
if maxL > 50 {
maxL = 50
errMsg = errMsg[:maxL] + "..."
}

maxL = len(responseBody)
if maxL > 500 {
maxL = 500
responseBody = responseBody[:maxL] + "..."
}
}

return provider.NewApplicationError(errType,
fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", statusCode, responseBody, errMsg))
}
Expand Down
144 changes: 144 additions & 0 deletions service/interpreter/activityImpl_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package interpreter

import (
"errors"
"fmt"
"github.com/golang/mock/gomock"
"github.com/indeedeng/iwf/gen/iwfidl"
"github.com/indeedeng/iwf/service/common/ptr"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -88,3 +94,141 @@ func createCommands() ([]iwfidl.TimerCommand, []iwfidl.SignalCommand, []iwfidl.I
}
return validTimerCommands, validSignalCommands, internalCommands
}

func TestComposeHttpError_LocalActivity_LongErrorResponse(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these test initializations are using duplicated logic as well. It'd be nice if these were consolidated. Maybe a function that takes the repeat length and returns the httpResp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to extract as much logic a to helper as I can

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

ctrl := gomock.NewController(t)
mockActivityProvider := NewMockActivityProvider(ctrl)

longError := strings.Repeat("a", 1000)

httpResp := &http.Response{
StatusCode: 400,
Body: io.NopCloser(strings.NewReader(longError)),
}
errMsg := "original error message"
err := errors.New(errMsg)

returnedError := errors.New("test error msg")
mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, longError[:50]+"...", errors.New(errMsg[:5]+"..."))).Return(returnedError)

err = composeHttpError(true, mockActivityProvider, err, httpResp, "test-error-type")
if err != nil {
return
}

assert.Error(t, err)
assert.Equal(t, returnedError, err)
}

func TestComposeHttpError_RegularActivity_LongErrorResponse(t *testing.T) {
ctrl := gomock.NewController(t)
mockActivityProvider := NewMockActivityProvider(ctrl)

longError := strings.Repeat("a", 1000)

httpResp := &http.Response{
StatusCode: 400,
Body: io.NopCloser(strings.NewReader(longError)),
}
errMsg := "original error message which is very long like this"
err := errors.New(errMsg)

returnedError := errors.New("test error msg")
mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, longError[:500]+"...", errors.New(errMsg[:50]+"..."))).Return(returnedError)

err = composeHttpError(false, mockActivityProvider, err, httpResp, "test-error-type")
if err != nil {
return
}

assert.Error(t, err)
assert.Equal(t, returnedError, err)
}

func TestComposeHttpError_LocalActivity_ShortErrorResponse(t *testing.T) {
ctrl := gomock.NewController(t)
mockActivityProvider := NewMockActivityProvider(ctrl)

shortError := strings.Repeat("a", 40)

httpResp := &http.Response{
StatusCode: 400,
Body: io.NopCloser(strings.NewReader(shortError)),
}
errMsg := "OK"
err := errors.New(errMsg)

returnedError := errors.New("test error msg")
mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, shortError, errors.New(errMsg))).Return(returnedError)

err = composeHttpError(true, mockActivityProvider, err, httpResp, "test-error-type")
if err != nil {
return
}

assert.Error(t, err)
assert.Equal(t, returnedError, err)
}

func TestComposeHttpError_RegularActivity_ShortErrorResponse(t *testing.T) {
ctrl := gomock.NewController(t)
mockActivityProvider := NewMockActivityProvider(ctrl)

shortError := strings.Repeat("a", 40)

httpResp := &http.Response{
StatusCode: 400,
Body: io.NopCloser(strings.NewReader(shortError)),
}
errMsg := "OK"
err := errors.New(errMsg)

returnedError := errors.New("test error msg")
mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, shortError, errors.New(errMsg))).Return(returnedError)

err = composeHttpError(false, mockActivityProvider, err, httpResp, "test-error-type")
if err != nil {
return
}

assert.Error(t, err)
assert.Equal(t, returnedError, err)
}

func TestComposeHttpError_LocalActivity_NilResponse(t *testing.T) {
ctrl := gomock.NewController(t)
mockActivityProvider := NewMockActivityProvider(ctrl)

errMsg := "OK"
err := errors.New(errMsg)

returnedError := errors.New("test error msg")
mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", 0, "None", errors.New(errMsg))).Return(returnedError)

err = composeHttpError(true, mockActivityProvider, err, nil, "test-error-type")
if err != nil {
return
}

assert.Error(t, err)
assert.Equal(t, returnedError, err)
}

func TestComposeHttpError_RegularActivity_NilResponse(t *testing.T) {
ctrl := gomock.NewController(t)
mockActivityProvider := NewMockActivityProvider(ctrl)

errMsg := "OK"
err := errors.New(errMsg)

returnedError := errors.New("test error msg")
mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", 0, "None", errors.New(errMsg))).Return(returnedError)

err = composeHttpError(false, mockActivityProvider, err, nil, "test-error-type")
if err != nil {
return
}

assert.Error(t, err)
assert.Equal(t, returnedError, err)
}
Loading
Loading