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

feat: return APIError from ReloadDeclarativeRawConfig #452

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
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
Loading