Skip to content

Commit

Permalink
feat: return APIError from ReloadDeclarativeRawConfig (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo authored Jun 25, 2024
1 parent 992aa54 commit c2f2a77
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@
- [0.2.0](#020)
- [0.1.0](#010)

## [v0.56.0]

> Release date: 2024/06/25
- `ReloadDeclarativeRawConfig` now returns `APIError` when it retrieves a valid
HTTP response, allowing inspection of the response status code.
[#452](https://github.com/Kong/go-kong/pull/452)

## [v0.55.0]

> Release date: 2024/05/14
Expand Down
2 changes: 1 addition & 1 deletion kong/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ func (c *Client) ReloadDeclarativeRawConfig(
return nil, fmt.Errorf("could not read /config %d status response body: %w", resp.StatusCode, err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
return b, fmt.Errorf("failed posting new config to /config: got status code %d", resp.StatusCode)
return b, NewAPIErrorWithRaw(resp.StatusCode, "failed posting new config to /config", b)
}

return b, nil
Expand Down
18 changes: 17 additions & 1 deletion kong/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"os"
"testing"
Expand Down Expand Up @@ -363,7 +364,8 @@ func TestReloadDeclarativeRawConfig(t *testing.T) {
body, err := client.ReloadDeclarativeRawConfig(ctx, bytes.NewBuffer(b), true, flattenErrors)

if tt.wantErr {
assert.Errorf(t, err, "Client.SendConfig() got unexpected error")
assert.Error(t, err)
require.IsType(t, err, &APIError{}, "expected APIError")
} else {
assert.NoError(t, err)
}
Expand All @@ -377,6 +379,20 @@ func TestReloadDeclarativeRawConfig(t *testing.T) {
}
}

func TestReloadDeclarativeRawConfig_NetworkErrorDoesntReturnAPIError(t *testing.T) {
RunWhenDBMode(t, "off")
SkipWhenKongRouterFlavor(t, Expressions)

// Point to a non-reachable URL so that we get a network error.
client, err := NewClient(String("http://non-reachable.url"), nil)
require.NoError(t, err)
require.NotNil(t, client)

_, err = client.ReloadDeclarativeRawConfig(context.Background(), bytes.NewReader([]byte("dummy-config")), true, true)
require.Error(t, err)
require.False(t, errors.Is(err, &APIError{}), "expected error to not be an APIError")
}

func assertHeadersExist(t *testing.T, request *http.Request, headers http.Header) {
for k, v := range headers {
assert.Contains(t, request.Header, k)
Expand Down

0 comments on commit c2f2a77

Please sign in to comment.