Fix missing error handling for server configuration endpoints

This commit is contained in:
Dane Everitt 2021-03-03 20:51:49 -08:00
parent 2a98faf360
commit 8897be661b
2 changed files with 16 additions and 15 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -12,6 +11,7 @@ import (
"strings" "strings"
"time" "time"
"emperror.dev/errors"
"github.com/apex/log" "github.com/apex/log"
"github.com/pterodactyl/wings/system" "github.com/pterodactyl/wings/system"
) )
@ -98,21 +98,19 @@ func (c *client) requestOnce(ctx context.Context, method, path string, body io.R
// request executes a http request and retries when errors occur. // request executes a http request and retries when errors occur.
// It appends the path to the endpoint of the client and adds the authentication token to the request. // It appends the path to the endpoint of the client and adds the authentication token to the request.
func (c *client) request(ctx context.Context, method, path string, body io.Reader, opts ...func(r *http.Request)) (*Response, error) { func (c *client) request(ctx context.Context, method, path string, body io.Reader, opts ...func(r *http.Request)) (res *Response, err error) {
var doErr error
var res *Response
for i := 0; i < c.retries; i++ { for i := 0; i < c.retries; i++ {
res, doErr = c.requestOnce(ctx, method, path, body, opts...) res, err = c.requestOnce(ctx, method, path, body, opts...)
if err == nil &&
if doErr == nil &&
res.StatusCode < http.StatusInternalServerError && res.StatusCode < http.StatusInternalServerError &&
res.StatusCode != http.StatusTooManyRequests { res.StatusCode != http.StatusTooManyRequests {
break break
} }
} }
if err != nil {
return res, doErr return nil, errors.WithStack(err)
}
return
} }
// get executes a http get request. // get executes a http get request.
@ -159,7 +157,7 @@ func (r *Response) HasError() bool {
func (r *Response) Read() ([]byte, error) { func (r *Response) Read() ([]byte, error) {
var b []byte var b []byte
if r.Response == nil { if r.Response == nil {
return nil, errors.New("no response exists on interface") return nil, errors.New("http: attempting to read missing response")
} }
if r.Response.Body != nil { if r.Response.Body != nil {
@ -180,7 +178,10 @@ func (r *Response) BindJSON(v interface{}) error {
return err return err
} }
return json.Unmarshal(b, &v) if err := json.Unmarshal(b, &v); err != nil {
return errors.Wrap(err, "http: could not unmarshal response")
}
return nil
} }
// Returns the first error message from the API call as a string. The error // Returns the first error message from the API call as a string. The error

View File

@ -75,7 +75,7 @@ func (c *client) GetServerConfiguration(ctx context.Context, uuid string) (Serve
defer res.Body.Close() defer res.Body.Close()
if res.HasError() { if res.HasError() {
return config, err return config, res.Error()
} }
err = res.BindJSON(&config) err = res.BindJSON(&config)
@ -90,7 +90,7 @@ func (c *client) GetInstallationScript(ctx context.Context, uuid string) (Instal
defer res.Body.Close() defer res.Body.Close()
if res.HasError() { if res.HasError() {
return InstallationScript{}, err return InstallationScript{}, res.Error()
} }
var config InstallationScript var config InstallationScript
@ -140,6 +140,7 @@ func (c *client) ValidateSftpCredentials(ctx context.Context, request SftpAuthRe
if err != nil { if err != nil {
return auth, err return auth, err
} }
defer res.Body.Close()
e := res.Error() e := res.Error()
if e != nil { if e != nil {
@ -185,7 +186,6 @@ func (c *client) SetBackupStatus(ctx context.Context, backup string, data Backup
return resp.Error() return resp.Error()
} }
// SendRestorationStatus triggers a request to the Panel to notify it that a // SendRestorationStatus triggers a request to the Panel to notify it that a
// restoration has been completed and the server should be marked as being // restoration has been completed and the server should be marked as being
// activated again. // activated again.