Replace error handling package with emperror; add better reporting for errors escaping server root

This commit is contained in:
Dane Everitt
2020-11-08 13:52:20 -08:00
parent 0989c78d4b
commit be9d1a3986
55 changed files with 396 additions and 367 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.WithStack(err)
return nil, errors.WithStackIf(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.WithStack(err)
return nil, errors.WithStackIf(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.WithStack(err)
return errors.WithStackIf(err)
}
return errors.WithStack(json.Unmarshal(b, &v))
return errors.WithStackIf(json.Unmarshal(b, &v))
}
// Returns the error message from the API call as a string. The error message will be formatted

View File

@@ -1,8 +1,8 @@
package api
import (
"emperror.dev/errors"
"fmt"
"github.com/pkg/errors"
"strconv"
)
@@ -16,7 +16,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.WithStack(err)
return nil, errors.WithStackIf(err)
}
defer resp.Body.Close()
@@ -26,7 +26,7 @@ func (r *Request) GetBackupRemoteUploadURLs(backup string, size int64) (*BackupR
var res BackupRemoteUploadResponse
if err := resp.Bind(&res); err != nil {
return nil, errors.WithStack(err)
return nil, errors.WithStackIf(err)
}
return &res, nil
@@ -44,7 +44,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.WithStack(err)
return errors.WithStackIf(err)
}
defer resp.Body.Close()

View File

@@ -2,10 +2,10 @@ package api
import (
"context"
"emperror.dev/errors"
"encoding/json"
"fmt"
"github.com/apex/log"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/config"
"golang.org/x/sync/errgroup"
"strconv"
@@ -57,7 +57,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.WithStack(err)
return nil, errors.WithStackIf(err)
}
defer resp.Body.Close()
@@ -67,7 +67,7 @@ func (r *Request) GetServers() ([]RawServerData, error) {
var res allServerResponse
if err := resp.Bind(&res); err != nil {
return nil, errors.WithStack(err)
return nil, errors.WithStackIf(err)
}
var mu sync.Mutex
@@ -117,7 +117,7 @@ func (r *Request) GetServers() ([]RawServerData, error) {
}
if err := g.Wait(); err != nil {
return nil, errors.WithStack(err)
return nil, errors.WithStackIf(err)
}
}
@@ -130,7 +130,7 @@ func (r *Request) GetServerConfiguration(uuid string) (ServerConfigurationRespon
resp, err := r.Get(fmt.Sprintf("/servers/%s", uuid), nil)
if err != nil {
return cfg, errors.WithStack(err)
return cfg, errors.WithStackIf(err)
}
defer resp.Body.Close()
@@ -139,7 +139,7 @@ func (r *Request) GetServerConfiguration(uuid string) (ServerConfigurationRespon
}
if err := resp.Bind(&cfg); err != nil {
return cfg, errors.WithStack(err)
return cfg, errors.WithStackIf(err)
}
return cfg, nil
@@ -150,7 +150,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.WithStack(err)
return is, errors.WithStackIf(err)
}
defer resp.Body.Close()
@@ -159,7 +159,7 @@ func (r *Request) GetInstallationScript(uuid string) (InstallationScript, error)
}
if err := resp.Bind(&is); err != nil {
return is, errors.WithStack(err)
return is, errors.WithStackIf(err)
}
return is, nil
@@ -169,7 +169,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.WithStack(err)
return errors.WithStackIf(err)
}
defer resp.Body.Close()
@@ -183,7 +183,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.WithStack(err)
return errors.WithStackIf(err)
}
defer resp.Body.Close()
@@ -193,7 +193,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.WithStack(err)
return errors.WithStackIf(err)
}
defer resp.Body.Close()
@@ -203,7 +203,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.WithStack(err)
return errors.WithStackIf(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"
)