Error handling improvements (#71)

* Remove `emperror.dev/errors`, remove all `errors#Wrap` and `errors#WithStack` calls
* Improve logging in `server/backup.go`
This commit is contained in:
Matthew Penner
2020-11-28 16:57:10 -07:00
committed by GitHub
parent 40c70673cd
commit de51fd1c51
55 changed files with 326 additions and 339 deletions

View File

@@ -2,10 +2,10 @@ package api
import (
"bytes"
"emperror.dev/errors"
"encoding/json"
"fmt"
"github.com/apex/log"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/system"
"io"
@@ -69,7 +69,7 @@ func (r *Request) Endpoint(endpoint string) string {
func (r *Request) Make(method, url string, body io.Reader, opts ...func(r *http.Request)) (*Response, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
req.Header.Set("User-Agent", fmt.Sprintf("Pterodactyl Wings/v%s (id:%s)", system.Version, config.Get().AuthenticationTokenId))
@@ -127,7 +127,7 @@ func (r *Request) Get(url string, data Q) (*Response, error) {
func (r *Request) Post(url string, data interface{}) (*Response, error) {
b, err := json.Marshal(data)
if err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
return r.Make(http.MethodPost, r.Endpoint(url), bytes.NewBuffer(b))
@@ -167,10 +167,10 @@ func (r *Response) Read() ([]byte, error) {
func (r *Response) Bind(v interface{}) error {
b, err := r.Read()
if err != nil {
return errors.WithStackIf(err)
return err
}
return errors.WithStackIf(json.Unmarshal(b, &v))
return json.Unmarshal(b, &v)
}
// Returns the error message from the API call as a string. The error message will be formatted

View File

@@ -1,7 +1,6 @@
package api
import (
"emperror.dev/errors"
"fmt"
"strconv"
)
@@ -16,7 +15,7 @@ type BackupRemoteUploadResponse struct {
func (r *Request) GetBackupRemoteUploadURLs(backup string, size int64) (*BackupRemoteUploadResponse, error) {
resp, err := r.Get(fmt.Sprintf("/backups/%s", backup), Q{"size": strconv.FormatInt(size, 10)})
if err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
defer resp.Body.Close()
@@ -26,7 +25,7 @@ func (r *Request) GetBackupRemoteUploadURLs(backup string, size int64) (*BackupR
var res BackupRemoteUploadResponse
if err := resp.Bind(&res); err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
return &res, nil
@@ -44,7 +43,7 @@ type BackupRequest struct {
func (r *Request) SendBackupStatus(backup string, data BackupRequest) error {
resp, err := r.Post(fmt.Sprintf("/backups/%s", backup), data)
if err != nil {
return errors.WithStackIf(err)
return err
}
defer resp.Body.Close()

View File

@@ -2,7 +2,6 @@ package api
import (
"context"
"emperror.dev/errors"
"encoding/json"
"fmt"
"github.com/apex/log"
@@ -57,7 +56,7 @@ type RawServerData struct {
func (r *Request) GetServers() ([]RawServerData, error) {
resp, err := r.Get("/servers", Q{"per_page": strconv.Itoa(int(config.Get().RemoteQuery.BootServersPerPage))})
if err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
defer resp.Body.Close()
@@ -67,7 +66,7 @@ func (r *Request) GetServers() ([]RawServerData, error) {
var res allServerResponse
if err := resp.Bind(&res); err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
var mu sync.Mutex
@@ -117,7 +116,7 @@ func (r *Request) GetServers() ([]RawServerData, error) {
}
if err := g.Wait(); err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
}
@@ -130,7 +129,7 @@ func (r *Request) GetServerConfiguration(uuid string) (ServerConfigurationRespon
resp, err := r.Get(fmt.Sprintf("/servers/%s", uuid), nil)
if err != nil {
return cfg, errors.WithStackIf(err)
return cfg, err
}
defer resp.Body.Close()
@@ -139,7 +138,7 @@ func (r *Request) GetServerConfiguration(uuid string) (ServerConfigurationRespon
}
if err := resp.Bind(&cfg); err != nil {
return cfg, errors.WithStackIf(err)
return cfg, err
}
return cfg, nil
@@ -150,7 +149,7 @@ func (r *Request) GetInstallationScript(uuid string) (InstallationScript, error)
var is InstallationScript
resp, err := r.Get(fmt.Sprintf("/servers/%s/install", uuid), nil)
if err != nil {
return is, errors.WithStackIf(err)
return is, err
}
defer resp.Body.Close()
@@ -159,7 +158,7 @@ func (r *Request) GetInstallationScript(uuid string) (InstallationScript, error)
}
if err := resp.Bind(&is); err != nil {
return is, errors.WithStackIf(err)
return is, err
}
return is, nil
@@ -169,7 +168,7 @@ func (r *Request) GetInstallationScript(uuid string) (InstallationScript, error)
func (r *Request) SendInstallationStatus(uuid string, successful bool) error {
resp, err := r.Post(fmt.Sprintf("/servers/%s/install", uuid), D{"successful": successful})
if err != nil {
return errors.WithStackIf(err)
return err
}
defer resp.Body.Close()
@@ -183,7 +182,7 @@ func (r *Request) SendInstallationStatus(uuid string, successful bool) error {
func (r *Request) SendArchiveStatus(uuid string, successful bool) error {
resp, err := r.Post(fmt.Sprintf("/servers/%s/archive", uuid), D{"successful": successful})
if err != nil {
return errors.WithStackIf(err)
return err
}
defer resp.Body.Close()
@@ -193,7 +192,7 @@ func (r *Request) SendArchiveStatus(uuid string, successful bool) error {
func (r *Request) SendTransferFailure(uuid string) error {
resp, err := r.Get(fmt.Sprintf("/servers/%s/transfer/failure", uuid), nil)
if err != nil {
return errors.WithStackIf(err)
return err
}
defer resp.Body.Close()
@@ -203,7 +202,7 @@ func (r *Request) SendTransferFailure(uuid string) error {
func (r *Request) SendTransferSuccess(uuid string) error {
resp, err := r.Get(fmt.Sprintf("/servers/%s/transfer/success", uuid), nil)
if err != nil {
return errors.WithStackIf(err)
return err
}
defer resp.Body.Close()

View File

@@ -1,8 +1,8 @@
package api
import (
"emperror.dev/errors"
"github.com/apex/log"
"github.com/pkg/errors"
"regexp"
)