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

fix: Override Code:0 with the StatusCode value #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func (c *amplitudeHTTPClient) Send(payload AmplitudePayload) AmplitudeResponse {
_ = json.Unmarshal(body, &amplitudeResponse)
} else {
c.logger.Debugf("HTTP response body is not valid JSON: %s", string(body))
amplitudeResponse.Code = response.StatusCode
}

amplitudeResponse.Status = response.StatusCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (t *AmplitudeHTTPClientSuiteSuite) TestSend_Empty() {

func (t *AmplitudeHTTPClientSuiteSuite) TestSend_Timeout() {
timeout := time.Millisecond * 100
server := t.createTestServer(timeout * 2, 200, `{"code": 234, "error": "some server error"}`)
server := t.createTestServer(timeout*2, 200, `{"code": 234, "error": "some server error"}`)

client := internal.NewAmplitudeHTTPClient(
server.URL,
Expand Down Expand Up @@ -172,7 +172,9 @@ func (t *AmplitudeHTTPClientSuiteSuite) TestSend_NonJsonResponse() {

t.Require().Equal(internal.AmplitudeResponse{
Status: 413,
Code: 413,
// amplitude_http_client always returns Code:0
// if response body cannot be parsed (not json).
Code: 0,
}, response)

server.Close()
Expand Down
12 changes: 8 additions & 4 deletions amplitude/plugins/destination/internal/amplitude_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import (
)

type AmplitudeResponse struct {
Status int `json:"-"`
Err error `json:"-"`

Code int `json:"code"`
// An HTTP Response Code
Status int `json:"-"`
// An HTTP Response Err
Err error `json:"-"`

// Code from the Response Body json
Code int `json:"code"`
// Error from the Response Body json
Error string `json:"error"`

MissingField string `json:"missing_field"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type amplitudeResponseProcessor struct {
func (p *amplitudeResponseProcessor) Process(events []*types.StorageEvent, response AmplitudeResponse) AmplitudeProcessorResult {
responseStatus := response.normalizedStatus()

if response.Code == 0 {
response.Code = responseStatus
}

var urlErr *url.Error
isURLErr := errors.As(response.Err, &urlErr)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,83 @@ func (t *AmplitudeResponseProcessorSuite) TestProcessUnknownError_ResponseError(
require.Equal(0, len(result.EventsForRetry))
}

func (t *AmplitudeResponseProcessorSuite) Test_Process_Overrides_Code_Eq_0() {
testCases := []struct {
name string
httpStatusCode int
expectCode int
}{
{
name: "StatusCode:200 - success",
httpStatusCode: 200,
expectCode: 200,
},
{
name: "StatusCode:299 - success",
httpStatusCode: 299,
expectCode: 200,
},
{
name: "StatusCode:100 - unexpected, returns -1",
httpStatusCode: 100,
expectCode: -1,
},
{
name: "StatusCode:429 - too many requests",
httpStatusCode: http.StatusTooManyRequests,
expectCode: 429,
},
{
name: "StatusCode:413 - request entity too large",
httpStatusCode: http.StatusRequestEntityTooLarge,
expectCode: 413,
},
{
name: "StatusCode:408 - request timeout",
httpStatusCode: http.StatusRequestTimeout,
expectCode: 408,
},
{
name: "StatusCode:400 - bad request",
httpStatusCode: http.StatusBadRequest,
expectCode: 400,
},
{
name: "StatusCode:418 - tea pot (and other unhandled 4xx)",
httpStatusCode: http.StatusTeapot,
expectCode: 400,
},
{
name: "StatusCode:500 - internal server error",
httpStatusCode: http.StatusInternalServerError,
expectCode: 500,
},
{
name: "StatusCode:502 - bad gateway (and other unhandled 5xx)",
httpStatusCode: http.StatusBadGateway,
expectCode: 500,
},
}

for _, tt := range testCases {
t.Run(tt.name, func() {
events := t.cloneOriginalEvents()
p := internal.NewAmplitudeResponseProcessor(internal.AmplitudeResponseProcessorOptions{
Now: time.Now,
Logger: noopLogger{},
})

result := p.Process(events, internal.AmplitudeResponse{
Status: tt.httpStatusCode,
// processor.Process must override Code=0
// with a normalized StatusCode
Code: 0,
})
t.Require().Equal(tt.expectCode, result.Code)
})
}
}

func (t *AmplitudeResponseProcessorSuite) cloneOriginalEvents() []*types.StorageEvent {
events := make([]*types.StorageEvent, len(originalEvents))

Expand Down