Skip to content

Commit

Permalink
Merge pull request #504 from Scalingo/fix/region-disabled-nice-msg
Browse files Browse the repository at this point in the history
Display a nice message when creating an app in a disabled region
  • Loading branch information
EtienneM authored Oct 10, 2019
2 parents ab2b0bc + 2ff6fef commit 9982433
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions apps/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func Create(appName string, remote string, buildpack string) error {

app, err := c.AppsCreate(scalingo.AppsCreateOpts{Name: appName})
if err != nil {
if utils.IsRegionDisabledError(err) {
return handleRegionDisabledError(appName, c)
}
if !utils.IsPaymentRequiredAndFreeTrialExceededError(err) {
return errgo.Mask(err, errgo.Any)
}
Expand All @@ -43,3 +46,31 @@ func Create(appName string, remote string, buildpack string) error {
}
return nil
}

func handleRegionDisabledError(appName string, c *scalingo.Client) error {
regions, rerr := c.RegionsList()
if rerr != nil {
return errgo.Notef(rerr, "region is disabled, failed to list available regions")
}
if len(regions) <= 1 {
return errgo.New("region is disabled and there is no other available region")
}
firstRegion := regions[0]
if firstRegion.Name == config.C.ScalingoRegion {
firstRegion = regions[1]
}

fmt.Printf("Application creation has been disabled on the currently used region: %v\n\n", config.C.ScalingoRegion)
fmt.Printf("Either configure your CLI to use another default region, then create your application:\n")
fmt.Printf(" scalingo config --region %s\n scalingo create %s\n", firstRegion.Name, appName)
fmt.Printf("\nOr use the region flag to specify the region explicitely for this command:\n")
fmt.Printf(" scalingo --region %s create %s\n", firstRegion.Name, appName)
fmt.Printf("\nList of available regions:\n")
for _, region := range regions {
if region.Name == config.C.ScalingoRegion {
continue
}
fmt.Printf("- %v (%v)\n", region.Name, region.DisplayName)
}
return nil
}
15 changes: 15 additions & 0 deletions utils/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

import (
"github.com/Scalingo/go-scalingo/http"
"github.com/Scalingo/go-utils/errors"
)

func IsRegionDisabledError(err error) bool {
reqerr, ok := errors.ErrgoRoot(err).(*http.RequestFailedError)
if !ok || reqerr.Code != 403 {
return false
}
httperr, ok := reqerr.APIError.(http.ForbiddenError)
return ok && httperr.Code == "region_disabled"
}
4 changes: 3 additions & 1 deletion vendor/github.com/Scalingo/go-scalingo/http/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9982433

Please sign in to comment.