Skip to content

Commit

Permalink
feat: return error response bodies in config svc
Browse files Browse the repository at this point in the history
  • Loading branch information
rainest committed Jan 31, 2023
1 parent dcf5a8b commit bc8191f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions kong/config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type AbstractConfigService interface {
// API endpoint using the provided reader which should contain the JSON
// serialized body that adheres to the configuration format specified at:
// https://docs.konghq.com/gateway/latest/production/deployment-topologies/db-less-and-declarative-config/#declarative-configuration-format
ReloadDeclarativeRawConfig(ctx context.Context, config io.Reader, checkHash bool) error
ReloadDeclarativeRawConfig(ctx context.Context, config io.Reader, checkHash bool) ([]byte, error)
}

// ConfigService handles Config in Kong.
Expand All @@ -27,7 +27,7 @@ func (c *ConfigService) ReloadDeclarativeRawConfig(
ctx context.Context,
config io.Reader,
checkHash bool,
) error {
) ([]byte, error) {
type sendConfigParams struct {
CheckHash int `url:"check_hash"`
}
Expand All @@ -37,29 +37,30 @@ func (c *ConfigService) ReloadDeclarativeRawConfig(
}
req, err := c.client.NewRequest("POST", "/config", sendConfigParams{CheckHash: checkHashI}, config)
if err != nil {
return fmt.Errorf("creating new HTTP request for /config: %w", err)
return fmt.Errorf("creating new HTTP request for /config: %w", err), []byte{}
}

resp, err := c.client.DoRAW(ctx, req)
if err != nil {
return fmt.Errorf("failed posting new config to /config: %w", err)
return fmt.Errorf("failed posting new config to /config: %w", err), []byte{}
}
defer resp.Body.Close()

var b []byte
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
b, err := io.ReadAll(resp.Body)
b, err = io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf(
"failed posting new config to /config: got status code %d (and failed to read the response body): %w",
resp.StatusCode, err,
)
), []byte{}
}

return fmt.Errorf(
"failed posting new config to /config: got status code %d, body: %s",
resp.StatusCode, b,
)
), b
}

return nil
return nil, b
}
2 changes: 1 addition & 1 deletion kong/config_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestConfigService(t *testing.T) {
b, err := json.Marshal(tt.config)
require.NoError(t, err)

if err := client.Configs.ReloadDeclarativeRawConfig(ctx, bytes.NewBuffer(b), true); (err != nil) != tt.wantErr {
if err, _ := client.Configs.ReloadDeclarativeRawConfig(ctx, bytes.NewBuffer(b), true); (err != nil) != tt.wantErr {
t.Errorf("Client.SendConfig() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down

0 comments on commit bc8191f

Please sign in to comment.