Skip to content

Commit

Permalink
Change response codes for elasticsearch failures to 503
Browse files Browse the repository at this point in the history
  • Loading branch information
michel-laterman committed Jan 24, 2024
1 parent 8ac12db commit 90f0c0b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Kind can be one of:
# - breaking-change: a change to previously-documented behavior
# - deprecation: functionality that is being removed in a later release
# - bug-fix: fixes a problem in a previous version
# - enhancement: extends functionality but does not break or fix existing behavior
# - feature: new functionality
# - known-issue: problems that we are aware of in a given version
# - security: impacts on the security of a product or a user’s deployment.
# - upgrade: important information for someone upgrading from a prior version
# - other: does not fit into any of the other categories
kind: enhancement

# Change summary; a 80ish characters long description of the change.
summary: Change response codes for elasticsearch failures to 503

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
#description:

# Affected component; a word indicating the component this changeset affects.
component:

# PR URL; optional; the PR number that added the changeset.
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
# Please provide it if you are adding a fragment for a different PR.
#pr: https://github.com/owner/repo/1234

# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
# If not present is automatically filled by the tooling with the issue linked to the PR number.
issue: 2852
10 changes: 10 additions & 0 deletions internal/pkg/api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@ func NewHTTPErrResp(err error) HTTPErrResp {
}
}

var esErr *es.ErrElastic
if errors.As(err, &esErr) {
return HTTPErrResp{
http.StatusServiceUnavailable,
esErr.Error(),
"elasticsearch error",
zerolog.ErrorLevel,
}
}

// Check if we have encountered a connectivity error
// Predicate taken from https://github.com/golang/go/blob/go1.17.5/src/net/dial_test.go#L798
if strings.Contains(err.Error(), "connection refused") {
Expand Down
29 changes: 29 additions & 0 deletions internal/pkg/api/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,32 @@ func Test_ErrorResp_NoTransaction(t *testing.T) {
require.Len(t, payloads.Transactions, 0)
require.Len(t, payloads.Errors, 0)
}

func Test_ErrResp_Status(t *testing.T) {
tests := []struct {
name string
err error
status int
}{{
name: "context canceled",
err: context.Canceled,
status: 499,
}, {
name: "generic error",
err: fmt.Errorf("some error"),
status: 500,
}, {
name: "es error",
err: &es.ErrElastic{
Status: 500,
},
status: 503,
}}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := NewHTTPErrResp(tc.err)
require.Equal(t, tc.status, r.StatusCode)
})
}
}

0 comments on commit 90f0c0b

Please sign in to comment.