Better error captures

This commit is contained in:
Dane Everitt 2020-08-27 19:57:22 -07:00
parent 68ab705aac
commit 0e474c8b24
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 13 additions and 11 deletions

View File

@ -26,7 +26,7 @@ type Archive struct {
func (a *Archive) Create(dst string, ctx context.Context) (os.FileInfo, error) { func (a *Archive) Create(dst string, ctx context.Context) (os.FileInfo, error) {
f, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) f, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
defer f.Close() defer f.Close()
@ -58,7 +58,7 @@ func (a *Archive) Create(dst string, ctx context.Context) (os.FileInfo, error) {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return errors.WithStack(ctx.Err())
default: default:
return a.addToArchive(p, tw) return a.addToArchive(p, tw)
} }
@ -75,12 +75,12 @@ func (a *Archive) Create(dst string, ctx context.Context) (os.FileInfo, error) {
log.WithField("location", dst).Warn("failed to delete corrupted backup archive") log.WithField("location", dst).Warn("failed to delete corrupted backup archive")
} }
return nil, err return nil, errors.WithStack(err)
} }
st, err := f.Stat() st, err := f.Stat()
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
return st, nil return st, nil
@ -91,6 +91,7 @@ func (a *Archive) addToArchive(p string, w *tar.Writer) error {
f, err := os.Open(p) f, err := os.Open(p)
if err != nil { if err != nil {
return err return err
return errors.WithStack(err)
} }
defer f.Close() defer f.Close()
@ -114,12 +115,12 @@ func (a *Archive) addToArchive(p string, w *tar.Writer) error {
defer a.Unlock() defer a.Unlock()
if err := w.WriteHeader(header); err != nil { if err := w.WriteHeader(header); err != nil {
return err return errors.WithStack(err)
} }
buf := make([]byte, 4*1024) buf := make([]byte, 4*1024)
if _, err := io.CopyBuffer(w, f, buf); err != nil { if _, err := io.CopyBuffer(w, f, buf); err != nil {
return err return errors.WithStack(err)
} }
return nil return nil

View File

@ -24,7 +24,7 @@ func LocateLocal(uuid string) (*LocalBackup, os.FileInfo, error) {
st, err := os.Stat(b.Path()) st, err := os.Stat(b.Path())
if err != nil { if err != nil {
return nil, nil, err return nil, nil, errors.WithStack(err)
} }
if st.IsDir() { 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 { if _, err := a.Create(b.Path(), context.Background()); err != nil {
return nil, err return nil, errors.WithStack(err)
} }
return b.Details(), nil return b.Details(), nil

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/apex/log" "github.com/apex/log"
"github.com/pkg/errors"
"io" "io"
"net/http" "net/http"
"os" "os"
@ -33,17 +34,17 @@ func (s *S3Backup) Generate(included *IncludedFiles, prefix string) (*ArchiveDet
} }
if _, err := a.Create(s.Path(), context.Background()); err != nil { if _, err := a.Create(s.Path(), context.Background()); err != nil {
return nil, err return nil, errors.WithStack(err)
} }
rc, err := os.Open(s.Path()) rc, err := os.Open(s.Path())
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
defer rc.Close() defer rc.Close()
if resp, err := s.generateRemoteRequest(rc); err != nil { if resp, err := s.generateRemoteRequest(rc); err != nil {
return nil, err return nil, errors.WithStack(err)
} else { } else {
resp.Body.Close() resp.Body.Close()