Skip to content

Commit

Permalink
Merge pull request #72 from Transfa/71-wrong-status-sent-in-case-of-e…
Browse files Browse the repository at this point in the history
…rrors

71 wrong status sent in case of errors
  • Loading branch information
koladev32 authored Jun 1, 2024
2 parents 26f46f9 + 2a90862 commit dbfb60c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - yyyy-mm-dd

- Wrong status sent in case of errors (#71)

### Added

### Fixed
Expand Down
6 changes: 3 additions & 3 deletions sendhooks/sender/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ var closeResponse = func(body io.ReadCloser) {
}
}

var processResponse = func(resp *http.Response) (string, []byte, error) {
var processResponse = func(resp *http.Response) (string, []byte, int, error) {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
logging.WebhookLogger(logging.ErrorType, fmt.Errorf("error reading response body: %s", err))
return "failed", nil, err
return "failed", nil, 0, err
}

status := "failed"
Expand All @@ -77,5 +77,5 @@ var processResponse = func(resp *http.Response) (string, []byte, error) {
logging.WebhookLogger(logging.ErrorType, fmt.Errorf("HTTP request failed with status code: %d, response body: %s", resp.StatusCode, string(respBody)))
}

return status, respBody, nil
return status, respBody, resp.StatusCode, nil
}
4 changes: 2 additions & 2 deletions sendhooks/sender/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func SendWebhook(data interface{}, url string, webhookId string, secretHash stri

defer closeResponse(resp.Body)

status, respBody, err := processResponse(resp)
status, respBody, statusCode, err := processResponse(resp)
if err != nil {
return err
}

message := fmt.Sprintf("webhook sending failed with status: %s, response body: %s", status, string(respBody))
message := fmt.Sprintf("webhook sending failed with status: %d, response body: %s", statusCode, string(respBody))

if status == "failed" {
logging.WebhookLogger(logging.WarningType, fmt.Errorf(message))
Expand Down
8 changes: 4 additions & 4 deletions sendhooks/sender/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func TestSendWebhook(t *testing.T) {

t.Run("Failed sendhooks due to response processing errors", func(t *testing.T) {
resetMocks()
processResponse = func(resp *http.Response) (string, []byte, error) {
return "failed", nil, errors.New("response processing error")
processResponse = func(resp *http.Response) (string, []byte, int, error) {
return "failed", nil, 0, errors.New("response processing error")
}

err := SendWebhook(nil, "http://dummy.com", "webhookId", "secretHash", redis.Configuration{})
Expand All @@ -67,8 +67,8 @@ func TestSendWebhook(t *testing.T) {

t.Run("Logging on failed sendhooks delivery", func(t *testing.T) {
resetMocks()
processResponse = func(resp *http.Response) (string, []byte, error) {
return "failed", []byte("error body"), nil
processResponse = func(resp *http.Response) (string, []byte, int, error) {
return "failed", []byte("error body"), 0, nil
}

SendWebhook(nil, "http://dummy\t\tassert.EqualError(t, err, \"failed\")\n.com", "webhookId", "secretHash", redis.Configuration{})
Expand Down

0 comments on commit dbfb60c

Please sign in to comment.