Skip to content

Commit

Permalink
builder/digitalocean: display friendler API error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pearkes committed Jul 1, 2013
1 parent 666a8e1 commit d9ab4e2
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions builder/digitalocean/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,28 +189,25 @@ func NewRequest(d DigitalOceanClient, path string, params string) (map[string]in
return decodedResponse, err
}

err = json.Unmarshal(body, &decodedResponse)

log.Printf("response from digitalocean: %v", decodedResponse)
log.Printf("response from digitalocean: %s", body)

// Catch all non-200 status and return an error
if resp.StatusCode != 200 {
err = errors.New(fmt.Sprintf("Received non-200 HTTP status from DigitalOcean: %v", resp.StatusCode))
return decodedResponse, err
}
err = json.Unmarshal(body, &decodedResponse)

// Check for bad JSON
if err != nil {
err = errors.New(fmt.Sprintf("Failed to decode JSON response (HTTP %v) from DigitalOcean: %s",
resp.StatusCode, body))
return decodedResponse, err
}

// Catch all non-OK statuses from DO and return an error
// Check for errors sent by digitalocean
status := decodedResponse["status"]
if status != "OK" {
// Get the actual error message if there is one
if status == "ERROR" {
status = decodedResponse["error_message"]
}
err = errors.New(fmt.Sprintf("Received bad status from DigitalOcean: %v", status))
err = errors.New(fmt.Sprintf("Received bad response (HTTP %v) from DigitalOcean: %s", resp.StatusCode, status))
return decodedResponse, err
}

Expand Down

0 comments on commit d9ab4e2

Please sign in to comment.