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:
@@ -4,7 +4,6 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"emperror.dev/errors"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/apex/log"
|
||||
@@ -13,6 +12,7 @@ import (
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/daemon/logger/jsonfilelog"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pterodactyl/wings/config"
|
||||
"github.com/pterodactyl/wings/environment"
|
||||
"io"
|
||||
@@ -36,7 +36,7 @@ func (e *Environment) Attach() error {
|
||||
}
|
||||
|
||||
if err := e.followOutput(); err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
opts := types.ContainerAttachOptions{
|
||||
@@ -48,7 +48,7 @@ func (e *Environment) Attach() error {
|
||||
|
||||
// Set the stream again with the container.
|
||||
if st, err := e.client.ContainerAttach(context.Background(), e.Id, opts); err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
} else {
|
||||
e.SetStream(&st)
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func (e *Environment) Attach() error {
|
||||
if err := e.pollResources(ctx); err != nil {
|
||||
l := log.WithField("environment_id", e.Id)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
l.WithField("error", errors.WithStackIf(err)).Error("error during environment resource polling")
|
||||
l.WithField("error", err).Error("error during environment resource polling")
|
||||
} else {
|
||||
l.Warn("stopping server resource polling: context canceled")
|
||||
}
|
||||
@@ -82,7 +82,7 @@ func (e *Environment) Attach() error {
|
||||
// Stream the reader output to the console which will then fire off events and handle console
|
||||
// throttling and sending the output to the user.
|
||||
if _, err := io.Copy(console, e.stream.Reader); err != nil {
|
||||
log.WithField("environment_id", e.Id).WithField("error", errors.WithStackIf(err)).Error("error while copying environment output to console")
|
||||
log.WithField("environment_id", e.Id).WithField("error", err).Error("error while copying environment output to console")
|
||||
}
|
||||
}(c)
|
||||
|
||||
@@ -120,7 +120,7 @@ func (e *Environment) InSituUpdate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
u := container.UpdateConfig{
|
||||
@@ -130,7 +130,7 @@ func (e *Environment) InSituUpdate() error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
if _, err := e.client.ContainerUpdate(ctx, e.Id, u); err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -145,12 +145,12 @@ func (e *Environment) Create() error {
|
||||
if _, err := e.client.ContainerInspect(context.Background(), e.Id); err == nil {
|
||||
return nil
|
||||
} else if !client.IsErrNotFound(err) {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Try to pull the requested image before creating the container.
|
||||
if err := e.ensureImageExists(e.meta.Image); err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
a := e.Configuration.Allocations()
|
||||
@@ -225,7 +225,7 @@ func (e *Environment) Create() error {
|
||||
}
|
||||
|
||||
if _, err := e.client.ContainerCreate(context.Background(), conf, hostConf, nil, e.Id); err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -277,7 +277,7 @@ func (e *Environment) Destroy() error {
|
||||
func (e *Environment) followOutput() error {
|
||||
if exists, err := e.Exists(); !exists {
|
||||
if err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return errors.New(fmt.Sprintf("no such container: %s", e.Id))
|
||||
@@ -302,7 +302,6 @@ func (e *Environment) followOutput() error {
|
||||
cr := []byte(" \r")
|
||||
crr := []byte("\r\n")
|
||||
|
||||
|
||||
// Avoid constantly re-allocating memory when we're flooding lines through this
|
||||
// function by using the same buffer for the duration of the call and just truncating
|
||||
// the value back to 0 every loop.
|
||||
@@ -354,7 +353,7 @@ func (e *Environment) followOutput() error {
|
||||
}
|
||||
}(reader)
|
||||
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Pulls the image from Docker. If there is an error while pulling the image from the source
|
||||
|
||||
@@ -2,7 +2,6 @@ package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"emperror.dev/errors"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/pterodactyl/wings/api"
|
||||
@@ -156,7 +155,7 @@ func (e *Environment) ExitState() (uint32, bool, error) {
|
||||
return 1, false, nil
|
||||
}
|
||||
|
||||
return 0, false, errors.WithStackIf(err)
|
||||
return 0, false, err
|
||||
}
|
||||
|
||||
return uint32(c.State.ExitCode), c.State.OOMKilled, nil
|
||||
|
||||
@@ -2,11 +2,11 @@ package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"emperror.dev/errors"
|
||||
"github.com/apex/log"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pterodactyl/wings/api"
|
||||
"github.com/pterodactyl/wings/environment"
|
||||
"os"
|
||||
@@ -26,7 +26,7 @@ func (e *Environment) OnBeforeStart() error {
|
||||
// the Panel is usee.
|
||||
if err := e.client.ContainerRemove(context.Background(), e.Id, types.ContainerRemoveOptions{RemoveVolumes: true}); err != nil {
|
||||
if !client.IsErrNotFound(err) {
|
||||
return errors.WrapIf(err, "failed to remove server docker container during pre-boot")
|
||||
return errors.WithMessage(err, "failed to remove server docker container during pre-boot")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ func (e *Environment) Start() error {
|
||||
//
|
||||
// @see https://github.com/pterodactyl/panel/issues/2000
|
||||
if !client.IsErrNotFound(err) {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// If the server is running update our internal state and continue on with the attach.
|
||||
@@ -84,7 +84,7 @@ func (e *Environment) Start() error {
|
||||
// to truncate them.
|
||||
if _, err := os.Stat(c.LogPath); err == nil {
|
||||
if err := os.Truncate(c.LogPath, 0); err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -99,14 +99,14 @@ func (e *Environment) Start() error {
|
||||
// exists on the system, and rebuild the container if that is required for server booting to
|
||||
// occur.
|
||||
if err := e.OnBeforeStart(); err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
|
||||
if err := e.client.ContainerStart(ctx, e.Id, types.ContainerStartOptions{}); err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// No errors, good to continue through.
|
||||
@@ -169,7 +169,7 @@ func (e *Environment) Stop() error {
|
||||
// will be terminated forcefully depending on the value of the second argument.
|
||||
func (e *Environment) WaitForStop(seconds uint, terminate bool) error {
|
||||
if err := e.Stop(); err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(seconds)*time.Second)
|
||||
@@ -185,10 +185,10 @@ func (e *Environment) WaitForStop(seconds uint, terminate bool) error {
|
||||
if terminate {
|
||||
log.WithField("container_id", e.Id).Info("server did not stop in time, executing process termination")
|
||||
|
||||
return errors.WithStackIf(e.Terminate(os.Kill))
|
||||
return e.Terminate(os.Kill)
|
||||
}
|
||||
|
||||
return errors.WithStackIf(ctxErr)
|
||||
return ctxErr
|
||||
}
|
||||
case err := <-errChan:
|
||||
if err != nil {
|
||||
@@ -197,13 +197,13 @@ func (e *Environment) WaitForStop(seconds uint, terminate bool) error {
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
l.Warn("deadline exceeded for container stop; terminating process")
|
||||
} else {
|
||||
l.WithField("error", errors.WithStackIf(err)).Warn("error while waiting for container stop; terminating process")
|
||||
l.WithField("error", err).Warn("error while waiting for container stop; terminating process")
|
||||
}
|
||||
|
||||
return errors.WithStackIf(e.Terminate(os.Kill))
|
||||
return e.Terminate(os.Kill)
|
||||
}
|
||||
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
case <-ok:
|
||||
}
|
||||
@@ -215,7 +215,7 @@ func (e *Environment) WaitForStop(seconds uint, terminate bool) error {
|
||||
func (e *Environment) Terminate(signal os.Signal) error {
|
||||
c, err := e.client.ContainerInspect(context.Background(), e.Id)
|
||||
if err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if !c.State.Running {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"emperror.dev/errors"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pterodactyl/wings/environment"
|
||||
)
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@ package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"emperror.dev/errors"
|
||||
"encoding/json"
|
||||
"github.com/apex/log"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pterodactyl/wings/environment"
|
||||
"io"
|
||||
"math"
|
||||
@@ -25,7 +25,7 @@ func (e *Environment) pollResources(ctx context.Context) error {
|
||||
|
||||
stats, err := e.client.ContainerStats(context.Background(), e.Id, true)
|
||||
if err != nil {
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
defer stats.Body.Close()
|
||||
|
||||
@@ -34,13 +34,13 @@ func (e *Environment) pollResources(ctx context.Context) error {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return errors.WithStackIf(ctx.Err())
|
||||
return ctx.Err()
|
||||
default:
|
||||
var v *types.StatsJSON
|
||||
|
||||
if err := dec.Decode(&v); err != nil {
|
||||
if err != io.EOF {
|
||||
l.WithField("error", errors.WithStackIf(err)).Warn("error while processing Docker stats output for container")
|
||||
l.WithField("error", err).Warn("error while processing Docker stats output for container")
|
||||
} else {
|
||||
l.Debug("io.EOF encountered during stats decode, stopping polling...")
|
||||
}
|
||||
@@ -75,7 +75,7 @@ func (e *Environment) pollResources(ctx context.Context) error {
|
||||
}
|
||||
|
||||
if b, err := json.Marshal(st); err != nil {
|
||||
l.WithField("error", errors.WithStackIf(err)).Warn("error while marshaling stats object for environment")
|
||||
l.WithField("error", err).Warn("error while marshaling stats object for environment")
|
||||
} else {
|
||||
e.Events().Publish(environment.ResourceEvent, string(b))
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"emperror.dev/errors"
|
||||
"encoding/json"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pterodactyl/wings/environment"
|
||||
"strconv"
|
||||
)
|
||||
@@ -15,7 +15,7 @@ type dockerLogLine struct {
|
||||
Log string `json:"log"`
|
||||
}
|
||||
|
||||
var ErrNotAttached = errors.Sentinel("not attached to instance")
|
||||
var ErrNotAttached = errors.New("not attached to instance")
|
||||
|
||||
func (e *Environment) setStream(s *types.HijackedResponse) {
|
||||
e.mu.Lock()
|
||||
@@ -42,7 +42,7 @@ func (e *Environment) SendCommand(c string) error {
|
||||
|
||||
_, err := e.stream.Conn.Write([]byte(c + "\n"))
|
||||
|
||||
return errors.WithStackIf(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Reads the log file for the server. This does not care if the server is running or not, it will
|
||||
@@ -54,7 +54,7 @@ func (e *Environment) Readlog(lines int) ([]string, error) {
|
||||
Tail: strconv.Itoa(lines),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.WithStackIf(err)
|
||||
return nil, err
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user