From 514c16ccc8ebc0de0dc08611d90db0ff94384ed9 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 16 Dec 2019 20:34:58 -0800 Subject: [PATCH] Less obtuse error handling from API responses --- api/api.go | 55 ++++++++++++++++++++--------------------- api/server_endpoints.go | 5 +--- api/sftp_endpoints.go | 2 +- server/server.go | 4 +-- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/api/api.go b/api/api.go index 1843f2b..fefedda 100644 --- a/api/api.go +++ b/api/api.go @@ -2,8 +2,8 @@ package api import ( "bytes" + "encoding/json" "fmt" - "github.com/buger/jsonparser" "github.com/pkg/errors" "github.com/pterodactyl/wings/config" "go.uber.org/zap" @@ -24,12 +24,6 @@ type PanelRequest struct { Response *http.Response } -func IsRequestError (err error) bool { - _, ok := err.(*PanelRequest) - - return ok -} - // Builds the base request instance that can be used with the HTTP client. func (r *PanelRequest) GetClient() *http.Client { return &http.Client{Timeout: time.Second * 30} @@ -38,7 +32,7 @@ func (r *PanelRequest) GetClient() *http.Client { func (r *PanelRequest) SetHeaders(req *http.Request) *http.Request { req.Header.Set("Accept", "application/vnd.pterodactyl.v1+json") req.Header.Set("Content-Type", "application/json") - req.Header.Set("Authorization", "Bearer " + config.Get().AuthenticationToken) + req.Header.Set("Authorization", "Bearer "+config.Get().AuthenticationToken) return req } @@ -116,29 +110,34 @@ func (r *PanelRequest) HttpResponseCode() int { return r.Response.StatusCode } +type RequestError struct { + Code string `json:"code"` + Status string `json:"status"` + Detail string `json:"detail"` +} + +// Returns the error response in a string form that can be more easily consumed. +func (re *RequestError) String() string { + return fmt.Sprintf("%s: %s (HTTP/%s)", re.Code, re.Detail, re.Status) +} + +type RequestErrorBag struct { + Errors []RequestError `json:"errors"` +} + // Returns the error message from the API call as a string. The error message will be formatted // similar to the below example: // // HttpNotFoundException: The requested resource does not exist. (HTTP/404) -func (r *PanelRequest) Error() string { - body, err := r.ReadBody() - if err != nil { - return err.Error() +func (r *PanelRequest) Error() *RequestError { + body, _ := r.ReadBody() + + bag := RequestErrorBag{} + json.Unmarshal(body, &bag) + + if len(bag.Errors) == 0 { + return new(RequestError) } - zap.S().Debugw("got body", zap.ByteString("b", body)) - _, valueType, _, err := jsonparser.Get(body, "errors") - if err != nil { - return err.Error() - } - - if valueType != jsonparser.Object { - return "no error object present on response" - } - - code, _ := jsonparser.GetString(body, "errors.0.code") - status, _ := jsonparser.GetString(body, "errors.0.status") - detail, _ := jsonparser.GetString(body, "errors.0.detail") - - return fmt.Sprintf("%s: %s (HTTP/%s)", code, detail, status) -} \ No newline at end of file + return &bag.Errors[0] +} diff --git a/api/server_endpoints.go b/api/server_endpoints.go index 886ef70..d463906 100644 --- a/api/server_endpoints.go +++ b/api/server_endpoints.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/pkg/errors" "github.com/pterodactyl/wings/parser" - "go.uber.org/zap" ) const ( @@ -40,9 +39,7 @@ func (r *PanelRequest) GetServerConfiguration(uuid string) (*ServerConfiguration r.Response = resp if r.HasError() { - zap.S().Warnw("got error", zap.String("message", r.Error())) - - return nil, errors.WithStack(errors.New(r.Error())) + return nil, errors.WithStack(errors.New(r.Error().String())) } res := &ServerConfiguration{} diff --git a/api/sftp_endpoints.go b/api/sftp_endpoints.go index b790321..a755373 100644 --- a/api/sftp_endpoints.go +++ b/api/sftp_endpoints.go @@ -25,7 +25,7 @@ func (r *PanelRequest) ValidateSftpCredentials(request sftp_server.Authenticatio return nil, sftp_server.InvalidCredentialsError{} } - return nil, errors.WithStack(errors.New(r.Error())) + return nil, errors.WithStack(errors.New(r.Error().String())) } response := new(sftp_server.AuthenticationResponse) diff --git a/server/server.go b/server/server.go index 6b1af0d..53c4edd 100644 --- a/server/server.go +++ b/server/server.go @@ -173,13 +173,13 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error { b, err := ioutil.ReadFile(path.Join(dir, file.Name())) if err != nil { - zap.S().Errorw("failed to read server configuration file, skipping...", zap.Error(err)) + zap.S().Errorw("failed to read server configuration file, skipping...", zap.String("server", file.Name()), zap.Error(err)) return } s, err := FromConfiguration(b, cfg) if err != nil { - zap.S().Errorw("failed to parse server configuration, skipping...", zap.Error(err)) + zap.S().Errorw("failed to parse server configuration, skipping...", zap.String("server", file.Name()), zap.Error(err)) return }