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

Commit

Permalink
Merge pull request #28 from compose/whitelists
Browse files Browse the repository at this point in the history
We've tidied the more complex error handling to properly populate the error array. This should have no impact on applications, but file an issue if it does.
  • Loading branch information
codepope authored Nov 9, 2017
2 parents 24f4468 + acb5c33 commit 5b7aced
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 122 deletions.
20 changes: 3 additions & 17 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package composeapi

import (
"encoding/json"
"fmt"
)

// Account structure
Expand Down Expand Up @@ -97,15 +96,9 @@ func (c *Client) CreateAccountUserJSON(accountID string, params UserParams) (str
End()

if response.StatusCode != 201 { // Expect Created on success
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s",
response.StatusCode, body))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
}

Expand All @@ -128,14 +121,7 @@ func (c *Client) DeleteAccountUserJSON(accountID, userID string) (string, []erro
End()

if response.StatusCode != 200 { // Expect OK on success
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s",
response.StatusCode, body))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down
9 changes: 2 additions & 7 deletions audit_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,9 @@ func (c *Client) GetAuditEventJSON(id string) (string, []error) {
End()

if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s", response.StatusCode, response.Body))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
}

Expand Down
17 changes: 2 additions & 15 deletions backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package composeapi

import (
"encoding/json"
"fmt"
)

// Backup structure
Expand Down Expand Up @@ -62,13 +61,7 @@ func (c *Client) StartBackupForDeploymentJSON(deploymentid string) (string, []er
End()

if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d", response.StatusCode))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down Expand Up @@ -149,13 +142,7 @@ func (c *Client) RestoreBackupJSON(params RestoreBackupParams) (string, []error)
End()

if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s", response.StatusCode, response.Body))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down
32 changes: 27 additions & 5 deletions composeapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ type Errors struct {
Error map[string][]string `json:"errors,omitempty"`
}

//SimpleError struct for parsing simple error returns
type SimpleError struct {
Error string `json:"errors"`
}

func printJSON(jsontext string) {
var tempholder map[string]interface{}

Expand All @@ -119,13 +124,30 @@ func (c *Client) getJSON(endpoint string) (string, []error) {
response, body, errs := c.newRequest("GET", apibase+endpoint).End()

if response.StatusCode != 200 {
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
}

//ProcessErrors tries to turn errors into an Errors struct
func ProcessErrors(statuscode int, body string) []error {
errs := []error{}
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
// Did parsing like this break anything
if err != nil {
mysimpleerror := SimpleError{}
err := json.Unmarshal([]byte(body), &mysimpleerror)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s", response.StatusCode, response.Body))
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s", statuscode, body))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
errs = append(errs, fmt.Errorf("%s", mysimpleerror.Error))
}
} else {
// Todo: iterate through and add eachg error.
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
return body, errs

return errs
}
10 changes: 1 addition & 9 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package composeapi

import (
"encoding/json"
"fmt"
)

//Version structure
Expand Down Expand Up @@ -58,14 +57,7 @@ func (c *Client) UpdateVersionJSON(deploymentID string, version string) (string,
End()

if response.StatusCode != 200 { // Expect OK on success - assume error on anything else
myErrors := Errors{}
err := json.Unmarshal([]byte(body), &myErrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s",
response.StatusCode, body))
} else {
errs = append(errs, fmt.Errorf("%v", myErrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down
16 changes: 2 additions & 14 deletions deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,7 @@ func (c *Client) DeprovisionDeploymentJSON(deploymentID string) (string, []error
End()

if response.StatusCode != 202 { // Expect Accepted on success - assume error on anything else
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s", response.StatusCode, response.Body))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down Expand Up @@ -265,13 +259,7 @@ func (c *Client) PatchDeploymentJSON(params PatchDeploymentParams) (string, []er
End()

if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s", response.StatusCode, response.Body))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down
9 changes: 1 addition & 8 deletions scalings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package composeapi

import (
"encoding/json"
"fmt"
)

// Scalings represents the used, allocated, starting and minimum unit scale
Expand Down Expand Up @@ -75,13 +74,7 @@ func (c *Client) SetScalingsJSON(params ScalingsParams) (string, []error) {
End()

if response.StatusCode != 200 { // Expect Accepted on success - assume error on anything else
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s", response.StatusCode, response.Body))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down
9 changes: 1 addition & 8 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,7 @@ func (c *Client) updateClusterTagsJSON(clusterID, method string, tags []string)
End()

if response.StatusCode != 200 { // Expect OK on success - assume error on anything else
myerrors := Errors{}
err := json.Unmarshal([]byte(body), &myerrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s",
response.StatusCode, response.Body))
} else {
errs = append(errs, fmt.Errorf("%v", myerrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down
9 changes: 1 addition & 8 deletions team_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,7 @@ func (c *Client) DeleteTeamRoleJSON(deploymentID string, params TeamRoleParams)
End()

if response.StatusCode != 204 { // No response body is returned on success
myErrors := Errors{}
err := json.Unmarshal([]byte(body), &myErrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s",
response.StatusCode, body))
} else {
errs = append(errs, fmt.Errorf("%v", myErrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return errs
Expand Down
35 changes: 4 additions & 31 deletions teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,7 @@ func (c *Client) CreateTeamJSON(params TeamParams) (string, []error) {
End()

if response.StatusCode != 201 { // Expect Created on success - assume error on anything else
myErrors := Errors{}
err := json.Unmarshal([]byte(body), &myErrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s",
response.StatusCode, response.Body))
} else {
errs = append(errs, fmt.Errorf("%v", myErrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}
return body, errs
}
Expand Down Expand Up @@ -145,14 +138,7 @@ func (c *Client) DeleteTeamJSON(teamID string) (string, []error) {
End()

if response.StatusCode != 200 { // Expect OK on success - assume error on anything else
myErrors := Errors{}
err := json.Unmarshal([]byte(body), &myErrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s",
response.StatusCode, body))
} else {
errs = append(errs, fmt.Errorf("%v", myErrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down Expand Up @@ -180,14 +166,7 @@ func (c *Client) PatchTeamJSON(teamID, teamName string) (string, []error) {
End()

if response.StatusCode != 200 { // Expect OK on success - assume error on anything else
myErrors := Errors{}
err := json.Unmarshal([]byte(body), &myErrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s",
response.StatusCode, body))
} else {
errs = append(errs, fmt.Errorf("%v", myErrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down Expand Up @@ -215,13 +194,7 @@ func (c *Client) PutTeamUsersJSON(teamID string, userIDs []string) (string, []er
End()

if response.StatusCode != 200 { // Expect OK on success - assume error on anything else
myErrors := Errors{}
err := json.Unmarshal([]byte(body), &myErrors)
if err != nil {
errs = append(errs, fmt.Errorf("Unable to parse error - status code %d - body %s", response.StatusCode, body))
} else {
errs = append(errs, fmt.Errorf("%v", myErrors.Error))
}
errs = ProcessErrors(response.StatusCode, body)
}

return body, errs
Expand Down

0 comments on commit 5b7aced

Please sign in to comment.