all repos — homestead @ 4c07535d9120fb999b0c008f1866972865f424f7

Code for my website

replace tozd/errors with Southclaws/fault

Alan Pearce
commit

4c07535d9120fb999b0c008f1866972865f424f7

parent

90a4ac43b915abb7a2949f4b5313b40d705b6071

1 file changed, 19 insertions(+), 11 deletions(-)

changed files
M internal/storage/files/file.gointernal/storage/files/file.go
@@ -1,6 +1,7 @@
package files import ( + "errors" "fmt" "hash/fnv" "io"
@@ -9,7 +10,8 @@ "os"
"path/filepath" "strings" - "gitlab.com/tozd/go/errors" + "github.com/Southclaws/fault" + "github.com/Southclaws/fault/fmsg" "go.alanpearce.eu/homestead/internal/buffer" "go.alanpearce.eu/homestead/internal/storage" )
@@ -20,25 +22,25 @@ "gzip": ".gz",
"zstd": ".zstd", } -func (r *Reader) OpenFile(path string, filename string) (*storage.File, errors.E) { +func (r *Reader) OpenFile(path string, filename string) (*storage.File, error) { f, err := os.Open(filename) if err != nil { - return nil, errors.WithMessage(err, "could not open file for reading") + return nil, fault.Wrap(err, fmsg.With("could not open file for reading")) } defer f.Close() stat, err := f.Stat() if err != nil { - return nil, errors.WithMessage(err, "could not stat file") + return nil, fault.Wrap(err, fmsg.With("could not stat file")) } buf := new(buffer.Buffer) if _, err := f.WriteTo(buf); err != nil { - return nil, errors.WithMessage(err, "could not read file") + return nil, fault.Wrap(err, fmsg.With("could not read file")) } etag, err := etag(buf) if err != nil { - return nil, errors.WithMessage(err, "could not calculate etag") + return nil, fault.Wrap(err, fmsg.With("could not calculate etag")) } file := &storage.File{
@@ -53,7 +55,7 @@ }
err = file.CalculateStyleHash() if err != nil { - return nil, errors.WithStack(err) + return nil, fault.Wrap(err) } for enc, suffix := range encodings {
@@ -63,11 +65,17 @@ if errors.Is(err, os.ErrNotExist) {
continue } - return nil, errors.WithMessagef(err, "could not stat file %s", filename+suffix) + return nil, fault.Wrap( + err, + fmsg.With(fmt.Sprintf("could not stat file %s", filename+suffix)), + ) } bytes, err := os.ReadFile(filename + suffix) if err != nil { - return nil, errors.WithMessagef(err, "could not read file %s", filename+suffix) + return nil, fault.Wrap( + err, + fmsg.With(fmt.Sprintf("could not read file %s", filename+suffix)), + ) } buf := buffer.NewBuffer(bytes) file.Encodings[enc] = buf
@@ -76,10 +84,10 @@
return file, nil } -func etag(f io.Reader) (string, errors.E) { +func etag(f io.Reader) (string, error) { hash := fnv.New64a() if _, err := io.Copy(hash, f); err != nil { - return "", errors.WithMessage(err, "could not hash file") + return "", fault.Wrap(err, fmsg.With("could not hash file")) } return fmt.Sprintf(`W/"%x"`, hash.Sum(nil)), nil