From 8897be661bc30cb2fd0f144ca1fadc16de1ab947 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Wed, 3 Mar 2021 20:51:49 -0800 Subject: [PATCH] Fix missing error handling for server configuration endpoints --- remote/http.go | 25 +++++++++++++------------ remote/servers.go | 6 +++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/remote/http.go b/remote/http.go index 7b4483a..b75cd9e 100644 --- a/remote/http.go +++ b/remote/http.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "io" "io/ioutil" @@ -12,6 +11,7 @@ import ( "strings" "time" + "emperror.dev/errors" "github.com/apex/log" "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. // 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) { - var doErr error - var res *Response - +func (c *client) request(ctx context.Context, method, path string, body io.Reader, opts ...func(r *http.Request)) (res *Response, err error) { for i := 0; i < c.retries; i++ { - res, doErr = c.requestOnce(ctx, method, path, body, opts...) - - if doErr == nil && + res, err = c.requestOnce(ctx, method, path, body, opts...) + if err == nil && res.StatusCode < http.StatusInternalServerError && res.StatusCode != http.StatusTooManyRequests { break } } - - return res, doErr + if err != nil { + return nil, errors.WithStack(err) + } + return } // get executes a http get request. @@ -159,7 +157,7 @@ func (r *Response) HasError() bool { func (r *Response) Read() ([]byte, error) { var b []byte 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 { @@ -180,7 +178,10 @@ func (r *Response) BindJSON(v interface{}) error { 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 diff --git a/remote/servers.go b/remote/servers.go index d24ae79..ef48caa 100644 --- a/remote/servers.go +++ b/remote/servers.go @@ -75,7 +75,7 @@ func (c *client) GetServerConfiguration(ctx context.Context, uuid string) (Serve defer res.Body.Close() if res.HasError() { - return config, err + return config, res.Error() } err = res.BindJSON(&config) @@ -90,7 +90,7 @@ func (c *client) GetInstallationScript(ctx context.Context, uuid string) (Instal defer res.Body.Close() if res.HasError() { - return InstallationScript{}, err + return InstallationScript{}, res.Error() } var config InstallationScript @@ -140,6 +140,7 @@ func (c *client) ValidateSftpCredentials(ctx context.Context, request SftpAuthRe if err != nil { return auth, err } + defer res.Body.Close() e := res.Error() if e != nil { @@ -185,7 +186,6 @@ func (c *client) SetBackupStatus(ctx context.Context, backup string, data Backup return resp.Error() } - // SendRestorationStatus triggers a request to the Panel to notify it that a // restoration has been completed and the server should be marked as being // activated again.