Skip to content

Commit

Permalink
fold error responses into error value
Browse files Browse the repository at this point in the history
  • Loading branch information
javierguerragiraldez committed Mar 16, 2023
1 parent 96716e6 commit 237e1e7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 10 additions & 0 deletions kong/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ func (c *Client) Do(ctx context.Context, req *http.Request,
return response, err
}

func ErrorOrResponseError(res *Response, err error) error {
if err != nil {
return err
}
if res.StatusCode >= http.StatusBadRequest { // errors start at 400
return fmt.Errorf("unexpected response: %q", res.Status)
}
return nil
}

// SetDebugMode enables or disables logging of
// the request to the logger set by SetLogger().
// By default, debug logging is disabled.
Expand Down
8 changes: 4 additions & 4 deletions kong/degraphql_route_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *DegraphqlRouteService) Create(ctx context.Context, route *DegraphqlRout
}

var createdRoute DegraphqlRoute
_, err = s.client.Do(ctx, req, &createdRoute)
err = ErrorOrResponseError(s.client.Do(ctx, req, &createdRoute))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func (s *DegraphqlRouteService) Get(
}

var route DegraphqlRoute
_, err = s.client.Do(ctx, req, &route)
err = ErrorOrResponseError(s.client.Do(ctx, req, &route))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (s *DegraphqlRouteService) Update(ctx context.Context, route *DegraphqlRout
}

var updatedRoute DegraphqlRoute
_, err = s.client.Do(ctx, req, &updatedRoute)
err = ErrorOrResponseError(s.client.Do(ctx, req, &updatedRoute))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func (s *DegraphqlRouteService) Delete(
return err
}

_, err = s.client.Do(ctx, req, nil)
err = ErrorOrResponseError(s.client.Do(ctx, req, nil))
return err
}

Expand Down
8 changes: 4 additions & 4 deletions kong/graphql_rate_limiting_cost_decoration_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *GraphqlRateLimitingCostDecorationService) Create(
}

var createdCostDeco GraphqlRateLimitingCostDecoration
_, err = s.client.Do(ctx, req, &createdCostDeco)
err = ErrorOrResponseError(s.client.Do(ctx, req, &createdCostDeco))
if err != nil {
return nil, err
}
Expand All @@ -64,7 +64,7 @@ func (s *GraphqlRateLimitingCostDecorationService) Get(
}

var costDeco GraphqlRateLimitingCostDecoration
_, err = s.client.Do(ctx, req, &costDeco)
err = ErrorOrResponseError(s.client.Do(ctx, req, &costDeco))
if err != nil {
return nil, err
}
Expand All @@ -89,7 +89,7 @@ func (s *GraphqlRateLimitingCostDecorationService) Update(
}

var updatedAPI GraphqlRateLimitingCostDecoration
_, err = s.client.Do(ctx, req, &updatedAPI)
err = ErrorOrResponseError(s.client.Do(ctx, req, &updatedAPI))
if err != nil {
return nil, err
}
Expand All @@ -112,7 +112,7 @@ func (s *GraphqlRateLimitingCostDecorationService) Delete(
return err
}

_, err = s.client.Do(ctx, req, nil)
err = ErrorOrResponseError(s.client.Do(ctx, req, nil))
return err
}

Expand Down

0 comments on commit 237e1e7

Please sign in to comment.