Skip to content

Commit

Permalink
provide better error messages
Browse files Browse the repository at this point in the history
use `message` field from response as error message
  • Loading branch information
caguiclajmg committed Jun 27, 2022
1 parent 1e2f386 commit 1f75033
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
5 changes: 3 additions & 2 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

type Response struct {
Success bool `json:"success"`
Success bool `json:"success"`
Error string `json:"error"`
}

type GetServerResponse struct {
Expand Down Expand Up @@ -107,7 +108,7 @@ func (client *Client) do(method string, path string, params map[string]string, h
// FIXME: Workaround for API issue which causes endpoint to
// return an HTML Page with a 200 Status code
if strings.HasPrefix(res.Header.Get("Content-Type"), "text/html") {
bytes, err := json.Marshal(Response{Success: false})
bytes, err := json.Marshal(Response{Success: false, Error: "api call failed"})
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions commands/billing.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package commands

import (
"errors"
"fmt"
"log"

"github.com/spf13/cobra"
)
Expand All @@ -18,8 +18,7 @@ var (
}

if !res.Success {
log.Printf("endpoint returned error")
return nil
return errors.New(res.Error)
}

fmt.Printf(`Balance: %v
Expand Down
16 changes: 8 additions & 8 deletions commands/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func serverList(cmd *cobra.Command, args []string) error {
}

if !res.Success {
return errors.New("endpoint returned error")
return errors.New(res.Error)
}

t := table.NewWriter()
Expand All @@ -129,7 +129,7 @@ func serverInfo(cmd *cobra.Command, args []string) error {
}

if !res.Success {
return errors.New("endpoint returned error")
return errors.New(res.Error)
}

props := []map[string]string{
Expand Down Expand Up @@ -172,7 +172,7 @@ func startServer(cmd *cobra.Command, args []string) error {
}

if !res.Success {
return errors.New("endpoint returned error")
return errors.New(res.Error)
}

return nil
Expand All @@ -186,7 +186,7 @@ func stopServer(cmd *cobra.Command, args []string) error {
}

if !res.Success {
return errors.New("endpoint returned error")
return errors.New(res.Error)
}

return nil
Expand All @@ -200,7 +200,7 @@ func deleteServer(cmd *cobra.Command, args []string) error {
}

if !res.Success {
return errors.New("endpoint returned error")
return errors.New(res.Error)
}

return nil
Expand Down Expand Up @@ -278,7 +278,7 @@ func deployServer(cmd *cobra.Command, args []string) error {
}

if !res.Success {
return errors.New("endpoint returned error")
return errors.New(res.Error)
}

fmt.Println(res.Server.Id)
Expand All @@ -294,7 +294,7 @@ func manageServer(cmd *cobra.Command, args []string) error {
}

if !res.Success {
return errors.New("endpoint returned error")
return errors.New(res.Error)
}

err = browser.OpenURL(res.Server.Links["dashboard"]["href"])
Expand All @@ -317,7 +317,7 @@ func restartServer(cmd *cobra.Command, args []string) error {
}

if !res.Success {
return errors.New("endpoint returned error")
return errors.New(res.Error)
}

return nil
Expand Down

0 comments on commit 1f75033

Please sign in to comment.