Skip to content

Commit

Permalink
Improve the error message when HTTP/429 is returned, fix #127
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Sep 6, 2023
1 parent 9211b93 commit bee6616
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
10 changes: 9 additions & 1 deletion pkg/koyeb/errors/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ import (
func NewCLIErrorFromAPIError(what string, err error, resp *http.Response) *CLIError {
ret := &CLIError{
What: what,
Orig: err,
}

if resp.StatusCode == 429 {
ret.Why = "the Koyeb API returned an error HTTP/429: Too Many Requests because you have exceeded the rate limit"
ret.Solution = "Please try again in a few seconds."
return ret
}

ret.Orig = err

var genericErr *koyeb.GenericOpenAPIError
var unmarshalErr *json.UnmarshalTypeError
var urlError *url.Error
Expand Down
18 changes: 5 additions & 13 deletions pkg/koyeb/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,11 @@ func GetOrganizationToken(api koyeb.OrganizationApi, ctx context.Context, organi
body := make(map[string]interface{})
res, resp, err := api.SwitchOrganization(ctx, organizationId).Body(body).Execute()
if err != nil {
errBuf := make([]byte, 1024)
// if the body can't be read, it won't be displayed in the error below
resp.Body.Read(errBuf) // nolint:errcheck
return "", &errors.CLIError{
What: "Error while switching the current organization",
Why: fmt.Sprintf("the API endpoint which switches the current organization returned an error %d", resp.StatusCode),
Additional: []string{
"You provided an organization id with the --organization flag, or the `organization` field is set in your configuration file.",
"The value provided is likely incorrect, or you don't have access to this organization.",
},
Orig: fmt.Errorf("HTTP/%d\n\n%s", resp.StatusCode, errBuf),
Solution: "List your organizations with `koyeb --organization=\"\" organization list`, then switch to the organization you want to use with `koyeb --organization=\"\" organization switch <id>`. Finally you can run your commands again, without the --organization flag.",
}
return "", errors.NewCLIErrorFromAPIError(
fmt.Sprintf("Error while switching the context to organization `%s`", organizationId),
err,
resp,
)
}
return *res.Token.Id, nil
}

0 comments on commit bee6616

Please sign in to comment.