Skip to content

Commit

Permalink
fix: errors in some API resources weren't being propagated (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows authored Mar 17, 2023
1 parent 64095d0 commit ac72d8e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
23 changes: 20 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ func (c *Client) ReadTeam(t broker.Team) (*broker.Team, error) {
// CreateTeam creates a Team
func (c *Client) CreateTeam(t broker.TeamCreateOrUpdateRequest) (*broker.Team, error) {
res, err := c.doCrud("POST", teamCreateTemplate, t, new(broker.TeamsResponse))

if err != nil {
return nil, err
}

apiResponse := res.(*broker.TeamsResponse)

// TODO: why is this a collection not a single resource?
Expand All @@ -166,14 +171,17 @@ func (c *Client) CreateTeam(t broker.TeamCreateOrUpdateRequest) (*broker.Team, e
// ReadTeamAssignments finds all users currently in a team
func (c *Client) ReadTeamAssignments(t broker.Team) (*broker.TeamsAssignmentResponse, error) {
res, err := c.doCrud("GET", urlEncodeTemplate(teamAssignmentTemplate, t.UUID), t, new(broker.TeamsAssignmentResponse))
apiResponse := res.(*broker.TeamsAssignmentResponse)

return apiResponse, err
return res.(*broker.TeamsAssignmentResponse), err
}

// UpdateTeamAssignments sets the users for a given team, removing any existing users not in the specified request
func (c *Client) UpdateTeamAssignments(r broker.TeamsAssignmentRequest) (*broker.TeamsAssignmentResponse, error) {
res, err := c.doCrud("PUT", urlEncodeTemplate(teamAssignmentTemplate, r.UUID), r, new(broker.TeamsAssignmentResponse))

if err != nil {
return nil, err
}

if len(r.Users) > 0 {
apiResponse := res.(*broker.TeamsAssignmentResponse)

Expand All @@ -186,6 +194,11 @@ func (c *Client) UpdateTeamAssignments(r broker.TeamsAssignmentRequest) (*broker
// AppendTeamAssignments adds users to an existing Team (does not remove absent ones)
func (c *Client) AppendTeamAssignments(r broker.TeamsAssignmentRequest) (*broker.TeamsAssignmentResponse, error) {
res, err := c.doCrud("POST", urlEncodeTemplate(teamAssignmentTemplate, r.UUID), r, new(broker.TeamsAssignmentResponse))

if err != nil {
return nil, err
}

if len(r.Users) > 0 {
apiResponse := res.(*broker.TeamsAssignmentResponse)

Expand Down Expand Up @@ -269,6 +282,10 @@ func (c *Client) CreateUser(u broker.User) (*broker.User, error) {
func (c *Client) CreateSystemAccount(u broker.User) (*broker.User, error) {
res, err := c.doCrud("POST", systemAccountCreateTemplate, u, nil)

if err != nil {
return nil, err
}

// Returns a 201
// e.g. https://tf-acceptance.pactflow.io/admin/system-accounts/f996d7e5-6525-4649-b479-9299793d105e
// + a list of users
Expand Down
2 changes: 1 addition & 1 deletion client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type apiErrorDescriptions []string

type APIKeyedError map[apiErrorKey]apiErrorDescriptions

// apiErrorResponse represents a body of the shape: {"errors":{"parameter": ["parameter is invaled"]}}
// apiErrorResponse represents a body of the shape: {"errors":{"parameter": ["parameter is invalid"]}}
type apiErrorResponse struct {
Errors APIKeyedError `json:"errors"`
Reference string `json:"reference"`
Expand Down

0 comments on commit ac72d8e

Please sign in to comment.