Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Fix error handling for profile loading and 4xx/5xx status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake Watters committed Jun 6, 2020
1 parent 28f9063 commit 60537e3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
5 changes: 5 additions & 0 deletions command/app_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@ func (s *AppLifecycleTestSuite) TestRunningAppStatusHelp() {
s.Require().NoError(err)
s.Require().Contains(output, "Check app status")
}

func (s *AppLifecycleTestSuite) TestRunningAppRestartNoSuchProfile() {
_, err := s.Execute("app", "-p", "invalid", "restart")
s.Require().Error(err, `no profile "invalid"`)
}
4 changes: 3 additions & 1 deletion command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ func (baseCmd *BaseCommand) initConfig() error {

// Load the configuration
if err := baseCmd.viperCfg.ReadInConfig(); err == nil {
baseCmd.LoadProfile()
if _, err = baseCmd.LoadProfile(); err != nil {
return err
}
} else {
// Ignore config file not found or error
var perr *os.PathError
Expand Down
34 changes: 26 additions & 8 deletions opsani/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ import (
"github.com/go-resty/resty/v2"
)

// APIError represents an error returned by the Opsani API
type APIError struct {
Status string `json:"status"`
Message string `json:"message"`
Traceback string `json:"traceback"`
Version string `json:"version"`
}

// Error returns an error representation of the API error
func (err APIError) Error() string {
return fmt.Sprintf("request failed: %s (%s)", err.Message, err.Status)
}

// Client provides a high level interface to the Opsani API
type Client struct {
restyClient *resty.Client
Expand All @@ -46,6 +59,19 @@ func NewClient() *Client {
"User-Agent": "Opsani CLI",
}).
SetHostURL("https://api.opsani.com")

// Return errors for 4xx and 5xx responses
rc.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
if resp.IsError() {
apiError := resp.Error().(*APIError)
if apiError != nil && *apiError != (APIError{}) {
return apiError
}
return fmt.Errorf("request failed (%q): %s", resp.Status(), resp.Body())
}

return nil
})
return createClientWithRestyClient(rc)
}

Expand Down Expand Up @@ -146,14 +172,6 @@ func (c *Client) PatchConfigFromBody(body interface{}, apply bool) (*resty.Respo
Lifecycle
*/

// APIError represents an error returned by the Opsani API
type APIError struct {
Status string `json:"status"`
Message string `json:"message"`
Traceback string `json:"traceback"`
Version string `json:"version"`
}

func (c *Client) stateURLPath() string {
return c.appResourceURLPath("state")
}
Expand Down

0 comments on commit 60537e3

Please sign in to comment.