Skip to content

Commit

Permalink
Merge pull request #397 from kool-dev/deploy-tweaks
Browse files Browse the repository at this point in the history
Deploy error handling tweaks
  • Loading branch information
fabriciojs authored Dec 5, 2021
2 parents 23b20a9 + 538f7d0 commit 94d5c8d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
6 changes: 3 additions & 3 deletions commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (d *KoolDeploy) Execute(args []string) (err error) {
api.SetBaseURL(url)
}

d.Shell().Println("Create release file...")
d.Shell().Info("Create release file...")
if filename, err = d.createReleaseFile(); err != nil {
return
}
Expand All @@ -89,14 +89,14 @@ func (d *KoolDeploy) Execute(args []string) (err error) {

deploy = api.NewDeploy(filename)

d.Shell().Println("Upload release file...")
d.Shell().Info("Upload release file...")
if err = deploy.SendFile(); err != nil {
return
}

d.Shell().Println("Going to deploy...")

timeout := 10 * time.Minute
timeout := 30 * time.Minute

if min, err := strconv.Atoi(d.env.Get("KOOL_API_TIMEOUT")); err == nil {
timeout = time.Duration(min) * time.Minute
Expand Down
1 change: 1 addition & 0 deletions services/cloud/api/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (d *Deploy) SendFile() (err error) {
if errAPI.Status == http.StatusUnauthorized {
err = ErrUnauthorized
} else if errAPI.Status == http.StatusUnprocessableEntity {
fmt.Println(err)
err = ErrPayloadValidation
} else if errAPI.Status != http.StatusOK && errAPI.Status != http.StatusCreated {
err = ErrBadResponseStatus
Expand Down
4 changes: 2 additions & 2 deletions services/cloud/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (e *DefaultEndpoint) DoCall() (err error) {
reqURL := fmt.Sprintf("%s/%s?%s", apiBaseURL, e.path, e.query.Encode())

if verbose {
fmt.Fprintf(os.Stderr, "api - calling URL: %s\n", reqURL)
fmt.Fprintf(os.Stderr, "[Kool Cloud] Going to call: %s\n", reqURL)
}

if request, err = http.NewRequest(e.method, reqURL, body); err != nil {
Expand All @@ -137,7 +137,7 @@ func (e *DefaultEndpoint) DoCall() (err error) {
}

if verbose {
fmt.Fprintf(os.Stderr, "api - got response: %s\n", string(raw))
fmt.Fprintf(os.Stderr, "[Kool Cloud] Got: %s\n", string(raw))
}

if e.statusCode >= 400 {
Expand Down
19 changes: 12 additions & 7 deletions services/cloud/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"errors"
"fmt"
"strings"
)

// ErrBadAPIServer represents some issue in the API side
Expand All @@ -17,7 +18,7 @@ var ErrDeployFailed error
// ErrUnauthorized unauthorized; please check your KOOL_API_TOKEN
var ErrUnauthorized error

// ErrPayloadValidation something went wrong validating the payload
// failed validating deploy payload
var ErrPayloadValidation error

// ErrBadResponseStatus unexpected return status
Expand All @@ -28,25 +29,29 @@ var ErrUnexpectedResponse error

// ErrAPI reprents a default error returned from the API
type ErrAPI struct {
Status int
Message string `json:"message"`
Status int

Errors map[string]interface{} `json:"errors"`
Message string `json:"message"`
Errors map[string]interface{} `json:"errors"`
}

// Error returns the string representation for the error
func (e *ErrAPI) Error() string {
if e.Errors != nil {
return fmt.Sprintf("%d - %s (%v)", e.Status, e.Message, e.Errors)
s := []string{}
for k, e := range e.Errors {
s = append(s, fmt.Sprintf("\t%s > %v", k, e.([]interface{})[0]))
}
return fmt.Sprintf("\n%d - %s\n\n%s\n", e.Status, e.Message, strings.Join(s, "\n"))
}
return fmt.Sprintf("%d - %s", e.Status, e.Message)
return fmt.Sprintf("\n%d - %s\n", e.Status, e.Message)
}

func init() {
ErrBadAPIServer = errors.New("bad API server response")
ErrDeployFailed = errors.New("deploy process has failed")
ErrUnauthorized = errors.New("unauthorized; please check your KOOL_API_TOKEN")
ErrPayloadValidation = errors.New("something went wrong validating the payload")
ErrPayloadValidation = errors.New("failed validating deploy payload")
ErrBadResponseStatus = errors.New("unexpected return status")
ErrUnexpectedResponse = errors.New("bad API response; please ask for support")
ErrMissingToken = errors.New("missing KOOL_API_TOKEN")
Expand Down
8 changes: 5 additions & 3 deletions services/cloud/api/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ func TestDefinedErrors(t *testing.T) {
func TestApiErr(t *testing.T) {
err := &ErrAPI{100, "message", nil}

if err.Error() != "100 - message" {
if err.Error() != "\n100 - message\n" {
t.Errorf("unexpected error message: %s", err.Error())
}

err.Errors = make(map[string]interface{})
err.Errors = map[string]interface{}{
"foo": []interface{}{"bar"},
}

if err.Error() != "100 - message (map[])" {
if err.Error() != "\n100 - message\n\n\tfoo > bar\n" {
t.Errorf("unexpected error message: %s", err.Error())
}
}

0 comments on commit 94d5c8d

Please sign in to comment.