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

@@ -3,9 +3,9 @@ package backup
import (
"archive/tar"
"context"
"emperror.dev/errors"
"github.com/apex/log"
gzip "github.com/klauspost/pgzip"
"github.com/pkg/errors"
"github.com/remeh/sizedwaitgroup"
"golang.org/x/sync/errgroup"
"io"
@@ -26,7 +26,7 @@ type Archive struct {
func (a *Archive) Create(dst string, ctx context.Context) error {
f, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return errors.WithStackIf(err)
return err
}
defer f.Close()
@@ -58,7 +58,7 @@ func (a *Archive) Create(dst string, ctx context.Context) error {
select {
case <-ctx.Done():
return errors.WithStackIf(ctx.Err())
return ctx.Err()
default:
return a.addToArchive(p, tw)
}
@@ -75,7 +75,7 @@ func (a *Archive) Create(dst string, ctx context.Context) error {
log.WithField("location", dst).Warn("failed to delete corrupted backup archive")
}
return errors.WithStackIf(err)
return err
}
return nil
@@ -91,7 +91,7 @@ func (a *Archive) addToArchive(p string, w *tar.Writer) error {
return nil
}
return errors.WithStackIf(err)
return err
}
defer f.Close()
@@ -102,14 +102,15 @@ func (a *Archive) addToArchive(p string, w *tar.Writer) error {
return nil
}
return errors.WithStackIf(err)
return err
}
header, err := tar.FileInfoHeader(s, strings.TrimPrefix(p, a.TrimPrefix))
name := strings.TrimPrefix(p, a.TrimPrefix)
header, err := tar.FileInfoHeader(s, name)
if err != nil {
return errors.WithStackIf(err)
return errors.WithMessage(err, "failed to get tar#FileInfoHeader for "+name)
}
header.Name = strings.TrimPrefix(p, a.TrimPrefix)
header.Name = name
// These actions must occur sequentially, even if this function is called multiple
// in parallel. You'll get some nasty panic's otherwise.
@@ -117,12 +118,12 @@ func (a *Archive) addToArchive(p string, w *tar.Writer) error {
defer a.Unlock()
if err := w.WriteHeader(header); err != nil {
return errors.WithStackIf(err)
return err
}
buf := make([]byte, 4*1024)
if _, err := io.CopyBuffer(w, io.LimitReader(f, header.Size), buf); err != nil {
return errors.WithStackIf(err)
return errors.WithMessage(err, "failed to copy "+header.Name+" to archive")
}
return nil

View File

@@ -2,7 +2,6 @@ package backup
import (
"crypto/sha1"
"emperror.dev/errors"
"encoding/hex"
"github.com/apex/log"
"github.com/pterodactyl/wings/api"
@@ -87,7 +86,7 @@ func (b *Backup) Path() string {
func (b *Backup) Size() (int64, error) {
st, err := os.Stat(b.Path())
if err != nil {
return 0, errors.WithStackIf(err)
return 0, err
}
return st.Size(), nil
@@ -99,7 +98,7 @@ func (b *Backup) Checksum() ([]byte, error) {
f, err := os.Open(b.Path())
if err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
defer f.Close()
@@ -128,6 +127,7 @@ func (b *Backup) Details() *ArchiveDetails {
"backup": b.Identifier(),
"error": err,
}).Error("failed to calculate checksum for backup")
return
}
checksum = hex.EncodeToString(resp)

View File

@@ -2,7 +2,7 @@ package backup
import (
"context"
"emperror.dev/errors"
"errors"
"os"
)
@@ -24,7 +24,7 @@ func LocateLocal(uuid string) (*LocalBackup, os.FileInfo, error) {
st, err := os.Stat(b.Path())
if err != nil {
return nil, nil, errors.WithStackIf(err)
return nil, nil, err
}
if st.IsDir() {
@@ -48,7 +48,7 @@ func (b *LocalBackup) Generate(included *IncludedFiles, prefix string) (*Archive
}
if err := a.Create(b.Path(), context.Background()); err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
return b.Details(), nil

View File

@@ -1,7 +1,7 @@
package backup
import (
"emperror.dev/errors"
"errors"
"fmt"
)

View File

@@ -3,7 +3,6 @@ package backup
import (
"bytes"
"context"
"emperror.dev/errors"
"fmt"
"github.com/apex/log"
"github.com/pterodactyl/wings/api"
@@ -31,20 +30,20 @@ func (s *S3Backup) Generate(included *IncludedFiles, prefix string) (*ArchiveDet
}
if err := a.Create(s.Path(), context.Background()); err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
rc, err := os.Open(s.Path())
if err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
defer rc.Close()
if err := s.generateRemoteRequest(rc); err != nil {
return nil, errors.WithStackIf(err)
return nil, err
}
return s.Details(), err
return s.Details(), nil
}
// Removes a backup from the system.