Skip to content

Commit

Permalink
cluster: handle definition download error (#2898)
Browse files Browse the repository at this point in the history
Handle non-200 HTTP status code in a graceful way, returning it instead of potentially unmarshalling-related errors.

category: bug
ticket: #2897

Closes #2897.
  • Loading branch information
gsora authored Feb 27, 2024
1 parent 2de8fe6 commit 639492d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cluster/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func FetchDefinition(ctx context.Context, url string) (Definition, error) {
if err != nil {
return Definition{}, errors.Wrap(err, "fetch file")
}

if resp.StatusCode/100 != 2 {
return Definition{}, errors.New("http error", z.Int("status_code", resp.StatusCode))
}

defer resp.Body.Close()

buf, err := io.ReadAll(resp.Body)
Expand Down
8 changes: 8 additions & 0 deletions cluster/helpers_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func TestFetchDefinition(t *testing.T) {
case "/invalidDef":
b, _ := invalidDef.MarshalJSON()
_, _ = w.Write(b)
case "/nonok":
w.WriteHeader(http.StatusInternalServerError)
}
}))
defer server.Close()
Expand All @@ -109,6 +111,12 @@ func TestFetchDefinition(t *testing.T) {
want: invalidDef,
wantErr: true,
},
{
name: "HTTP status is not in the 200 range",
url: fmt.Sprintf("%s/%s", server.URL, "nonok"),
want: invalidDef,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 639492d

Please sign in to comment.