Skip to content

Commit

Permalink
Fixed error in the code, added a new handler for network error
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Apr 2, 2021
1 parent ed132de commit c47419c
Show file tree
Hide file tree
Showing 23 changed files with 45 additions and 42 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func NewAdvancedClientForTesting(responses map[string]map[string]string) (*Clien
}
}

if responseSent == false {
if !responseSent {
fmt.Println("Failed to find a matching request!")
fmt.Println("Request body:", string(body))
fmt.Println("Method:", req.Method)
Expand All @@ -139,7 +139,7 @@ func NewClientForTesting(responses map[string]string) (*Client, *httptest.Server
}
}

if responseSent == false {
if !responseSent {
fmt.Println("Failed to find a matching request!")
fmt.Println("URL:", req.URL.String())

Expand Down
2 changes: 1 addition & 1 deletion dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c *Client) FindDNSDomain(search string) (*DNSDomain, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
19 changes: 11 additions & 8 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,18 @@ func decodeERROR(err error) error {
var msg strings.Builder

switch err := err.(type) {
case *url.Error:
if _, ok := err.Err.(net.Error); ok {
err := fmt.Errorf("we found a problem connected against the api")
return TimeoutError.wrap(err)
}
case net.Error:
if err.Timeout() {
err := fmt.Errorf("We have a network issue")
err := fmt.Errorf("we found a network issue")
return TimeoutError.wrap(err)
}
case *url.Error:
fmt.Println("This is a *url.Error")
if err, ok := err.Err.(net.Error); ok && err.Timeout() {
err := fmt.Errorf("We have a network issue")
if _, ok := err.(*net.DNSError); ok {
err := fmt.Errorf("we found a dns issue")
return TimeoutError.wrap(err)
}
case wrapError:
Expand All @@ -308,19 +311,19 @@ func decodeERROR(err error) error {
byt := []byte(errorData.Reason)

if err := json.Unmarshal(byt, &dat); err != nil {
err := errors.New("Failed to decode the response expected from the API")
err := errors.New("failed to decode the response expected from the API")
return ResponseDecodeFailedError.wrap(err)
}

if _, ok := dat["status"].(float64); ok {
if dat["status"].(float64) == 500 {
err := errors.New("Internal Server Error")
err := errors.New("internal Server Error")
return InternalServerError.wrap(err)
}
}

if dat["result"] == "requires_authentication" {
err := errors.New("Authentication Error")
err := errors.New("authentication Error")
return AuthenticationError.wrap(err)
}

Expand Down
2 changes: 1 addition & 1 deletion firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *Client) FindFirewall(search string) (*Firewall, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
2 changes: 1 addition & 1 deletion instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (c *Client) FindInstance(search string) (*Instance, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Hostname, search) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
2 changes: 1 addition & 1 deletion instance_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Client) FindInstanceSizes(search string) (*InstanceSize, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
2 changes: 1 addition & 1 deletion instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func TestListInstances(t *testing.T) {
client, server, err := NewClientForTesting(map[string]string{
client, server, _ := NewClientForTesting(map[string]string{
"/v2/instances": `{"page": 1, "per_page": 20, "pages": 2, "items":[{"id": "12345", "hostname": "foo.example.com"}]}`,
})
defer server.Close()
Expand Down
2 changes: 1 addition & 1 deletion kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (c *Client) FindKubernetesCluster(search string) (*KubernetesCluster, error
exactMatch = true
result = value
} else if strings.Contains(strings.ToUpper(value.Name), strings.ToUpper(search)) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
4 changes: 2 additions & 2 deletions kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestFindKubernetesCluster(t *testing.T) {
})
defer server.Close()

got, err := client.FindKubernetesCluster("69a23478")
got, _ := client.FindKubernetesCluster("69a23478")
if got.ID != "69a23478-a89e-41d2-97b1-6f4c341cee70" {
t.Errorf("Expected %s, got %s", "69a23478-a89e-41d2-97b1-6f4c341cee70", got.ID)
}
Expand All @@ -150,7 +150,7 @@ func TestFindKubernetesCluster(t *testing.T) {
t.Errorf("Expected %s, got %s", "d1cd0b71-5da1-492e-9d0d-a46ccdaae2fa", got.ID)
}

_, err = client.FindKubernetesCluster("cluster")
_, err := client.FindKubernetesCluster("cluster")
if err.Error() != "MultipleMatchesError: unable to find cluster because there were multiple matches" {
t.Errorf("Expected %s, got %s", "unable to find cluster because there were multiple matches", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *Client) FindLoadBalancer(search string) (*LoadBalancer, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Hostname, search) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
4 changes: 2 additions & 2 deletions loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestFindLoadBalancer(t *testing.T) {
})
defer server.Close()

got, err := client.FindLoadBalancer("542e9eca")
got, _ := client.FindLoadBalancer("542e9eca")
if got.ID != "542e9eca-539d-45e6-b629-2f905d0b5f93" {
t.Errorf("Expected %s, got %s", "542e9eca-539d-45e6-b629-2f905d0b5f93", got.ID)
}
Expand All @@ -87,7 +87,7 @@ func TestFindLoadBalancer(t *testing.T) {
t.Errorf("Expected %s, got %s", "c33051ae-f337-45de-a3a5-004d822deff5", got.ID)
}

_, err = client.FindLoadBalancer("example")
_, err := client.FindLoadBalancer("example")
if err.Error() != "MultipleMatchesError: unable to find example because there were multiple matches" {
t.Errorf("Expected %s, got %s", "unable to find example because there were multiple matches", err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func (c *Client) GetDefaultNetwork() (*Network, error) {
}

networks := make([]Network, 0)
err = json.NewDecoder(bytes.NewReader(resp)).Decode(&networks)
json.NewDecoder(bytes.NewReader(resp)).Decode(&networks)
for _, network := range networks {
if network.Default {
return &network, nil
}
}

return nil, errors.New("No default network found")
return nil, errors.New("no default network found")
}

// NewNetwork creates a new private network
Expand Down Expand Up @@ -94,7 +94,7 @@ func (c *Client) FindNetwork(search string) (*Network, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) || strings.Contains(value.Label, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
6 changes: 3 additions & 3 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestFindNetwork(t *testing.T) {
})
defer server.Close()

got, err := client.FindNetwork("34")
got, _ := client.FindNetwork("34")
if got.ID != "12345" {
t.Errorf("Expected %s, got %s", "12345", got.ID)
}
Expand All @@ -114,12 +114,12 @@ func TestFindNetwork(t *testing.T) {
t.Errorf("Expected %s, got %s", "67890", got.ID)
}

got, err = client.FindNetwork("production")
got, _ = client.FindNetwork("production")
if got.ID != "67890" {
t.Errorf("Expected %s, got %s", "67890", got.ID)
}

_, err = client.FindNetwork("net")
_, err := client.FindNetwork("net")
if err.Error() != "MultipleMatchesError: unable to find net because there were multiple matches" {
t.Errorf("Expected %s, got %s", "unable to find net because there were multiple matches", err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions region.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *Client) FindRegion(search string) (*Region, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.Code, search) || strings.Contains(value.Country, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down Expand Up @@ -88,5 +88,5 @@ func (c *Client) GetDefaultRegion() (*Region, error) {
}
}

return nil, errors.New("No default region found")
return nil, errors.New("no default region found")
}
2 changes: 1 addition & 1 deletion snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *Client) FindSnapshot(search string) (*Snapshot, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
4 changes: 2 additions & 2 deletions snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestFindSnapshot(t *testing.T) {
})
defer server.Close()

got, err := client.FindSnapshot("ff39")
got, _ := client.FindSnapshot("ff39")
if got.ID != "0ca69adc-ff39-4fc1-8f08-d91434e86fac" {
t.Errorf("Expected %s, got %s", "0ca69adc-ff39-4fc1-8f08-d91434e86fac", got.ID)
}
Expand All @@ -151,7 +151,7 @@ func TestFindSnapshot(t *testing.T) {
t.Errorf("Expected %s, got %s", "aadec58e-26f4-43e7-8963-18739519ef76", got.ID)
}

_, err = client.FindSnapshot("backup")
_, err := client.FindSnapshot("backup")
if err.Error() != "MultipleMatchesError: unable to find backup because there were multiple matches" {
t.Errorf("Expected %s, got %s", "unable to find backup because there were multiple matches", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *Client) FindSSHKey(search string) (*SSHKey, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
4 changes: 2 additions & 2 deletions ssh_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestFindSSHKey(t *testing.T) {
})
defer server.Close()

got, err := client.FindSSHKey("34")
got, _ := client.FindSSHKey("34")
if got.ID != "12345" {
t.Errorf("Expected %s, got %s", "12345", got.ID)
}
Expand All @@ -61,7 +61,7 @@ func TestFindSSHKey(t *testing.T) {
t.Errorf("Expected %s, got %s", "12345", got.ID)
}

_, err = client.FindSSHKey("23")
_, err := client.FindSSHKey("23")
if err.Error() != "MultipleMatchesError: unable to find 23 because there were multiple matches" {
t.Errorf("Expected %s, got %s", "unable to find 23 because there were multiple matches", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (c *Client) FindTemplate(search string) (*Template, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Code, search) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
2 changes: 1 addition & 1 deletion volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *Client) FindVolume(search string) (*Volume, error) {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
4 changes: 2 additions & 2 deletions volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestFindVolume(t *testing.T) {
})
defer server.Close()

got, err := client.FindVolume("34")
got, _ := client.FindVolume("34")
if got.ID != "12345" {
t.Errorf("Expected %s, got %s", "12345", got.ID)
}
Expand All @@ -75,7 +75,7 @@ func TestFindVolume(t *testing.T) {
t.Errorf("Expected %s, got %s", "67890", got.ID)
}

_, err = client.FindVolume("volume")
_, err := client.FindVolume("volume")
if err.Error() != "MultipleMatchesError: unable to find volume because there were multiple matches" {
t.Errorf("Expected %s, got %s", "unable to find volume because there were multiple matches", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Client) FindWebhook(search string) (*Webhook, error) {
exactMatch = true
result = value
} else if strings.Contains(value.URL, search) || strings.Contains(value.ID, search) {
if exactMatch == false {
if !exactMatch {
result = value
partialMatchesCount++
}
Expand Down
4 changes: 2 additions & 2 deletions webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestFindWebhook(t *testing.T) {
})
defer server.Close()

got, err := client.FindWebhook("34")
got, _ := client.FindWebhook("34")
if got.ID != "12345" {
t.Errorf("Expected %s, got %s", "12345", got.ID)
}
Expand All @@ -125,7 +125,7 @@ func TestFindWebhook(t *testing.T) {
t.Errorf("Expected %s, got %s", "67890", got.ID)
}

_, err = client.FindWebhook("example")
_, err := client.FindWebhook("example")
if err.Error() != "MultipleMatchesError: unable to find example because there were multiple matches" {
t.Errorf("Expected %s, got %s", "unable to find example because there were multiple matches", err.Error())
}
Expand Down

0 comments on commit c47419c

Please sign in to comment.