Better error handling from responses

This commit is contained in:
Dane Everitt 2020-09-13 13:55:40 -07:00
parent f2a6d6b3c5
commit 6ba15e9884
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 17 additions and 9 deletions

View File

@ -137,6 +137,7 @@ func IsRequestError(err error) bool {
} }
type RequestError struct { type RequestError struct {
response *http.Response
Code string `json:"code"` Code string `json:"code"`
Status string `json:"status"` Status string `json:"status"`
Detail string `json:"detail"` Detail string `json:"detail"`
@ -144,7 +145,7 @@ type RequestError struct {
// Returns the error response in a string form that can be more easily consumed. // Returns the error response in a string form that can be more easily consumed.
func (re *RequestError) Error() string { func (re *RequestError) Error() string {
return fmt.Sprintf("Error response from Panel: %s: %s (HTTP/%s)", re.Code, re.Detail, re.Status) return fmt.Sprintf("Error response from Panel: %s: %s (HTTP/%d)", re.Code, re.Detail, re.response.StatusCode)
} }
func (re *RequestError) String() string { func (re *RequestError) String() string {
@ -165,9 +166,12 @@ func (r *PanelRequest) Error() *RequestError {
bag := RequestErrorBag{} bag := RequestErrorBag{}
json.Unmarshal(body, &bag) json.Unmarshal(body, &bag)
if len(bag.Errors) == 0 { e := new(RequestError)
return new(RequestError) if len(bag.Errors) > 0 {
e = &bag.Errors[0]
} }
return &bag.Errors[0] e.response = r.Response
return e
} }

View File

@ -68,6 +68,12 @@ func (r *PanelRequest) ValidateSftpCredentials(request SftpAuthRequest) (*SftpAu
if r.HasError() { if r.HasError() {
if r.HttpResponseCode() >= 400 && r.HttpResponseCode() < 500 { if r.HttpResponseCode() >= 400 && r.HttpResponseCode() < 500 {
log.WithFields(log.Fields{
"subsystem": "sftp",
"username": request.User,
"ip": request.IP,
}).Warn(r.Error().String())
return nil, new(sftpInvalidCredentialsError) return nil, new(sftpInvalidCredentialsError)
} }

View File

@ -203,7 +203,6 @@ func rootCmdRun(*cobra.Command, []string) {
s.Log().WithField("error", err).Error("error checking server environment status") s.Log().WithField("error", err).Error("error checking server environment status")
} }
fmt.Println(s.Id(), st, r, s.IsRunning(), s.GetState())
// Check if the server was previously running. If so, attempt to start the server now so that Wings // Check if the server was previously running. If so, attempt to start the server now so that Wings
// can pick up where it left off. If the environment does not exist at all, just create it and then allow // can pick up where it left off. If the environment does not exist at all, just create it and then allow
// the normal flow to execute. // the normal flow to execute.
@ -211,7 +210,6 @@ func rootCmdRun(*cobra.Command, []string) {
// This does mean that booting wings after a catastrophic machine crash and wiping out the Docker images // This does mean that booting wings after a catastrophic machine crash and wiping out the Docker images
// as a result will result in a slow boot. // as a result will result in a slow boot.
if !r && (st == environment.ProcessRunningState || st == environment.ProcessStartingState) { if !r && (st == environment.ProcessRunningState || st == environment.ProcessStartingState) {
fmt.Println("starting server, not running and should be")
if err := s.HandlePowerAction(server.PowerActionStart); err != nil { if err := s.HandlePowerAction(server.PowerActionStart); err != nil {
s.Log().WithField("error", errors.WithStack(err)).Warn("failed to return server to running state") s.Log().WithField("error", errors.WithStack(err)).Warn("failed to return server to running state")
} }