Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
feat(warden): error handled
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudheer Pal committed Oct 23, 2023
1 parent c284a9e commit 4677041
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
4 changes: 2 additions & 2 deletions internal/server/v1/iam/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand Down
32 changes: 21 additions & 11 deletions warden/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package warden
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

"github.com/goto/dex/pkg/errors"
)

type Client struct {
Expand All @@ -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())

Check failure on line 47 in warden/client.go

View workflow job for this annotation

GitHub Actions / golangci-lint

err113: do not define dynamic errors, use wrapped static errors instead: "errors.New(fmt.Errorf(\"got non-200 http status code=(%d) body=%s\", resp.StatusCode, bodyString).Error())" (goerr113)

Check failure on line 47 in warden/client.go

View workflow job for this annotation

GitHub Actions / golangci-lint

err113: do not define dynamic errors, use wrapped static errors instead: "errors.New(fmt.Errorf(\"got non-200 http status code=(%d) body=%s\", resp.StatusCode, bodyString).Error())" (goerr113)
}

var data teamListResponse
Expand All @@ -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())

Check failure on line 81 in warden/client.go

View workflow job for this annotation

GitHub Actions / golangci-lint

err113: do not define dynamic errors, use wrapped static errors instead: "errors.New(fmt.Errorf(\"got non-200 http status code=(%d) body=%s\", resp.StatusCode, bodyString).Error())" (goerr113)

Check failure on line 81 in warden/client.go

View workflow job for this annotation

GitHub Actions / golangci-lint

err113: do not define dynamic errors, use wrapped static errors instead: "errors.New(fmt.Errorf(\"got non-200 http status code=(%d) body=%s\", resp.StatusCode, bodyString).Error())" (goerr113)
}

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
Expand Down
6 changes: 3 additions & 3 deletions warden/error.go
Original file line number Diff line number Diff line change
@@ -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")
)

0 comments on commit 4677041

Please sign in to comment.