all repos — homestead @ 4c07535d9120fb999b0c008f1866972865f424f7

Code for my website

replace tozd/errors with Southclaws/fault

Alan Pearce
commit

4c07535d9120fb999b0c008f1866972865f424f7

parent

90a4ac43b915abb7a2949f4b5313b40d705b6071

1 file changed, 36 insertions(+), 27 deletions(-)

changed files
M internal/fetcher/fetcher.gointernal/fetcher/fetcher.go
@@ -2,6 +2,8 @@ package fetcher
import ( "context" + "errors" + "fmt" "io" "io/fs" "math"
@@ -13,8 +15,9 @@ "strconv"
"strings" "time" + "github.com/Southclaws/fault" + "github.com/Southclaws/fault/fmsg" "github.com/google/renameio/v2" - "gitlab.com/tozd/go/errors" "go.alanpearce.eu/homestead/internal/config" "go.alanpearce.eu/homestead/internal/events" "go.alanpearce.eu/x/log"
@@ -46,35 +49,35 @@ updater: options.Listener,
} } -func (f *Fetcher) getArtefacts(run uint64) errors.E { +func (f *Fetcher) getArtefacts(run uint64) error { runID := strconv.FormatUint(run, 10) f.log.Debug("getting artefacts", "run_id", runID) err := os.MkdirAll(filepath.Join(f.options.Root, runID), 0o750) if err != nil { - return errors.WithMessage(err, "could not create directory") + return fault.Wrap(err, fmsg.With("could not create directory")) } for _, file := range files { err := f.getFile(runID, file) if err != nil { - return errors.WithMessage(err, "could not fetch file") + return fault.Wrap(err, fmsg.With("could not fetch file")) } } f.current = run err = renameio.Symlink(runID, filepath.Join(f.options.Root, "current")) if err != nil { - return errors.WithMessage(err, "could not create/update symlink") + return fault.Wrap(err, fmsg.With("could not create/update symlink")) } return nil } -func (f *Fetcher) checkFolder() errors.E { +func (f *Fetcher) checkFolder() error { contents, err := os.ReadDir(f.options.Root) if err != nil { - return errors.WithMessage(err, "could not read root directory") + return fault.Wrap(err, fmsg.With("could not read root directory")) } var badFiles []string for _, f := range contents {
@@ -85,18 +88,18 @@ }
} if len(badFiles) > 0 { - return errors.WithStack( - errors.Basef("unexpected files in root directory: %s", strings.Join(badFiles, ", ")), + return fault.Wrap( + fault.Newf("unexpected files in root directory: %s", strings.Join(badFiles, ", ")), ) } return nil } -func (f *Fetcher) CleanOldRevisions() errors.E { +func (f *Fetcher) CleanOldRevisions() error { contents, err := os.ReadDir(f.options.Root) if err != nil { - return errors.WithMessage(err, "could not read root directory") + return fault.Wrap(err, fmsg.With("could not read root directory")) } for _, file := range contents { name := file.Name()
@@ -106,12 +109,15 @@ }
if numericFilename.MatchString(name) { v, err := strconv.ParseUint(name, 10, 64) if err != nil { - return errors.WithMessagef(err, "could not parse numeric filename %s", name) + return fault.Wrap( + err, + fmsg.With(fmt.Sprintf("could not parse numeric filename %s", name)), + ) } if v < f.current-1 { err := os.RemoveAll(filepath.Join(f.options.Root, name)) if err != nil { - return errors.WithMessage(err, "could not remove folder") + return fault.Wrap(err, fmsg.With("could not remove folder")) } } }
@@ -128,7 +134,7 @@ }
}() } -func (f *Fetcher) getFile(runID, basename string) errors.E { +func (f *Fetcher) getFile(runID, basename string) error { filename := filepath.Join(f.options.Root, runID, basename) url := f.options.FetchURL.JoinPath(runID, basename).String()
@@ -136,7 +142,7 @@ f.log.Debug("getting file", "filename", filename, "url", url)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0o600) if err != nil { - return errors.WithMessage(err, "could not open file") + return fault.Wrap(err, fmsg.With("could not open file")) } defer file.Close()
@@ -145,40 +151,43 @@ defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { - return errors.WithMessage(err, "could not create request") + return fault.Wrap(err, fmsg.With("could not create request")) } res, err := http.DefaultClient.Do(req) if err != nil { - return errors.WithMessage(err, "could not issue request") + return fault.Wrap(err, fmsg.With("could not issue request")) } _, err = io.Copy(file, res.Body) if err != nil { - return errors.WithMessage(err, "could not write file") + return fault.Wrap(err, fmsg.With("could not write file")) } err = file.Sync() if err != nil { - return errors.WithMessage(err, "could not sync file") + return fault.Wrap(err, fmsg.With("could not sync file")) } return nil } -func (f *Fetcher) getCurrentVersion() (uint64, errors.E) { +func (f *Fetcher) getCurrentVersion() (uint64, error) { target, err := os.Readlink(filepath.Join(f.options.Root, "current")) if err != nil && errors.Is(err, fs.ErrNotExist) { - return 0, errors.WithMessage(err, "could not stat current link") + return 0, fault.Wrap(err, fmsg.With("could not stat current link")) } f.current, err = strconv.ParseUint(target, 10, 64) if err != nil { - return 0, errors.WithMessagef(err, "unexpected symlink target (current -> %s)", target) + return 0, fault.Wrap( + err, + fmsg.With(fmt.Sprintf("unexpected symlink target (current -> %s)", target)), + ) } return f.current, nil } -func (f *Fetcher) initialiseStorage() (uint64, errors.E) { +func (f *Fetcher) initialiseStorage() (uint64, error) { latest, err := f.updater.GetLatestRunID() if err != nil { f.log.Warn("could not get latest run ID, using fallback", "error", err)
@@ -190,7 +199,7 @@
if latest > f.current { err = f.getArtefacts(latest) if err != nil { - return latest, errors.WithMessage(err, "could not fetch artefacts") + return latest, fault.Wrap(err, fmsg.With("could not fetch artefacts")) } return latest, nil
@@ -199,7 +208,7 @@
return f.current, nil } -func (f *Fetcher) Subscribe() (<-chan string, errors.E) { +func (f *Fetcher) Subscribe() (<-chan string, error) { err := f.checkFolder() if err != nil { return nil, err
@@ -250,10 +259,10 @@
return ch, nil } -func (f *Fetcher) connect(root string, ch chan string) errors.E { +func (f *Fetcher) connect(root string, ch chan string) error { updates, err := f.updater.Subscribe() if err != nil { - return errors.WithMessage(err, "could not subscribe to updates") + return fault.Wrap(err, fmsg.With("could not subscribe to updates")) } go func() {