MCPcopy Create free account
hub / github.com/Scalingo/cli / displayRequestFailedError

Function displayRequestFailedError

cmd/error.go:105–153  ·  view source on GitHub ↗
(requestFailedErr *httpclient.RequestFailedError, currentUser *scalingo.User, autherr error, err error)

Source from the content-addressed store, hash-verified

103}
104
105func displayRequestFailedError(requestFailedErr *httpclient.RequestFailedError, currentUser *scalingo.User, autherr error, err error) {
106 switch requestFailedErr.Code {
107 case http.StatusBadRequest: // 400
108 // In case of bad request error, we only want to display the API error.
109 // The call to strings.ReplaceAll is useful as the API error message may contain a raw \n.
110 io.Errorf("%s\n", strings.ReplaceAll(requestFailedErr.Error(), `\n`, "\n"))
111
112 case http.StatusUnauthorized: // 401
113 if currentUser != nil {
114 io.Errorf("You are currently logged in as %s.\n", currentUser.Username)
115 io.Errorf("Are you sure %s is a collaborator of this app with sufficient privileges?\n", currentUser.Username)
116 } else {
117 io.Errorf("Failed to read credentials for current user: %v", autherr)
118 }
119
120 case http.StatusNotFound: // 404
121 // we can ignore the returned error as it is already checked earlier in the code
122 notFoundErr, _ := requestFailedErr.APIError.(httpclient.NotFoundError)
123 if notFoundErr.Resource == "app" {
124 // apiURL contains something like:
125 // "https://api.agora-fr1.scalingo.com/v1"
126 apiURL, _ := url.Parse(requestFailedErr.Req.URL)
127 region := strings.Split(apiURL.Host, ".")[1]
128 io.Errorf("The application was not found on the region %s.\n", region)
129 io.Error("You can try on a different region with 'scalingo --region osc-fr1 ...'.")
130 io.Error("")
131 io.Error("List of available regions for your account is accessible with 'scalingo regions'.")
132 } else {
133 io.Error("An error occurred:")
134 debug.Println(err)
135 fmt.Println(io.Indent(err.Error(), 7))
136 }
137
138 case http.StatusConflict: // 409
139 // In case of conflict error, we only want to display the API error.
140 io.Errorf("%s\n", strings.ReplaceAll(requestFailedErr.Error(), `\n`, "\n"))
141 case http.StatusUnprocessableEntity:
142 unprocessableEntityErr, ok := requestFailedErr.APIError.(httpclient.UnprocessableEntity)
143 if !ok {
144 // If the error returned by the API has not been correctly parsed by go-scalingo
145 displayScalingoError(err)
146 } else {
147 io.Errorf("422 Unprocessable Content\n%v\n", unprocessableEntityErr.Error())
148 }
149
150 default:
151 displayScalingoError(err)
152 }
153}
154
155func newReportError(currentUser *scalingo.User, err error) *ReportError {
156 r := &ReportError{

Callers 1

displayErrorFunction · 0.85

Calls 5

ErrorfFunction · 0.92
ErrorFunction · 0.92
IndentFunction · 0.92
displayScalingoErrorFunction · 0.85
ErrorMethod · 0.80

Tested by

no test coverage detected