diff --git a/internal/server/v1/iam/service.go b/internal/server/v1/iam/service.go index 20d2075..3327329 100644 --- a/internal/server/v1/iam/service.go +++ b/internal/server/v1/iam/service.go @@ -35,7 +35,7 @@ func (svc *Service) UserWardenTeamList(ctx context.Context, userEmail string) ([ Email: userEmail, }) if err != nil { - return nil, err + return nil, fmt.Errorf("error listing user teams: %w", err) } return teams, nil @@ -46,7 +46,7 @@ func (svc *Service) LinkGroupToWarden(ctx context.Context, groupID, wardenTeamID TeamUUID: wardenTeamID, }) if err != nil { - return nil, err + return nil, fmt.Errorf("error getting warden team: %w", err) } getGroupRes, err := svc.shieldClient.GetGroup(ctx, &shieldv1beta1.GetGroupRequest{ diff --git a/warden/client.go b/warden/client.go index 60e1e8a..8dfd393 100644 --- a/warden/client.go +++ b/warden/client.go @@ -3,10 +3,10 @@ package warden import ( "context" "encoding/json" + "errors" "fmt" + "io" "net/http" - - "github.com/goto/dex/pkg/errors" ) type Client struct { @@ -25,21 +25,26 @@ func (c *Client) ListUserTeams(ctx context.Context, req TeamListRequest) ([]Team url := fmt.Sprintf("%s/api/v1/users/%s/teams", c.BaseURL, req.Email) httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { - return nil, err + return nil, fmt.Errorf("error creating request: %w", err) } resp, err := c.Client.Do(httpReq) if err != nil { - return nil, err + return nil, fmt.Errorf("error sending request: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { if resp.StatusCode == http.StatusNotFound { - return nil, errors.ErrNotFound.WithMsgf("user with email %s not found", req.Email) + return nil, ErrUserEmailNotFound } - return nil, errors.ErrInternal.WithMsgf("failed to fetch teams: %v", resp.Status) + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("error reading response body: %w", err) + } + bodyString := string(bodyBytes) + return nil, errors.New(fmt.Errorf("got non-200 http status code=(%d) body=%s", resp.StatusCode, bodyString).Error()) } var data teamListResponse @@ -54,27 +59,32 @@ func (c *Client) TeamByUUID(ctx context.Context, req TeamByUUIDRequest) (*Team, url := fmt.Sprintf("%s/api/v2/teams/%s", c.BaseURL, req.TeamUUID) httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { - return nil, err + return nil, fmt.Errorf("error creating request: %w", err) } resp, err := c.Client.Do(httpReq) if err != nil { - return nil, err + return nil, fmt.Errorf("error sending request: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { if resp.StatusCode == http.StatusNotFound { - return nil, errors.ErrNotFound.WithMsgf("team with uuid %s not found", req.TeamUUID) + return nil, ErrTeamUUIDNotFound } - return nil, errors.ErrInternal.WithMsgf("failed to fetch teams: %v", resp.Status) + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("error reading response body: %w", err) + } + bodyString := string(bodyBytes) + return nil, errors.New(fmt.Errorf("got non-200 http status code=(%d) body=%s", resp.StatusCode, bodyString).Error()) } var data teamResponse err = json.NewDecoder(resp.Body).Decode(&data) if err != nil { - return nil, err + return nil, fmt.Errorf("error decoding teamResponse: %w", err) } return &data.Data, nil diff --git a/warden/error.go b/warden/error.go index a3e68ea..fcd1dea 100644 --- a/warden/error.go +++ b/warden/error.go @@ -1,8 +1,8 @@ package warden -import "github.com/goto/dex/pkg/errors" +import "errors" var ( - ErrOptimusHostNotFound = errors.New("No Optimus jobs found in this project") - ErrOptimusHostInvalid = errors.New("Optimus host is not valid") + ErrUserEmailNotFound = errors.New("user email not found") + ErrTeamUUIDNotFound = errors.New("team with uuid not found") )