replace tozd/errors with Southclaws/fault
1 file changed, 28 insertions(+), 27 deletions(-)
changed files
M internal/storage/files/writer.go → internal/storage/files/writer.go
@@ -12,9 +12,10 @@ "go.alanpearce.eu/homestead/internal/multifile" "go.alanpearce.eu/homestead/internal/storage" "go.alanpearce.eu/x/log" + "github.com/Southclaws/fault" + "github.com/Southclaws/fault/fmsg" "github.com/andybalholm/brotli" "github.com/klauspost/compress/zstd" - "gitlab.com/tozd/go/errors" ) const (@@ -32,7 +33,7 @@ type Options struct { Compress bool } -func NewWriter(outputDirectory string, logger *log.Logger, opts *Options) (*Files, errors.E) { +func NewWriter(outputDirectory string, logger *log.Logger, opts *Options) (*Files, error) { return &Files{ outputDirectory: outputDirectory, options: opts,@@ -40,49 +41,49 @@ log: logger, }, nil } -func (f *Files) OpenRead(filename string) (io.ReadCloser, errors.E) { +func (f *Files) OpenRead(filename string) (io.ReadCloser, error) { file, err := os.Open(f.join(filename)) if err != nil { - return nil, errors.WithStack(err) + return nil, fault.Wrap(err) } return file, nil } -func (f *Files) OpenWrite(filename string) (io.WriteCloser, errors.E) { +func (f *Files) OpenWrite(filename string) (io.WriteCloser, error) { return openFileWrite(f.join(filename)) } -func (f *Files) WritePost(post *content.Post, content *buffer.Buffer) errors.E { +func (f *Files) WritePost(post *content.Post, content *buffer.Buffer) error { fd, err := f.write(post.URL, content) if err != nil { return err } if err := fd.Close(); err != nil { - return errors.WithStack(err) + return fault.Wrap(err) } if mf, isMultifile := fd.(*multifile.MultiFile); isMultifile { err = mf.Chtimes(post.Date) } else { - err = errors.WithStack(os.Chtimes(fd.Name(), post.Date, post.Date)) + err = fault.Wrap(os.Chtimes(fd.Name(), post.Date, post.Date)) } if err != nil { - return errors.WithMessage(err, "could not set file times") + return fault.Wrap(err, fmsg.With("could not set file times")) } return nil } -func (f *Files) Write(pathname string, _ string, content *buffer.Buffer) errors.E { +func (f *Files) Write(pathname string, _ string, content *buffer.Buffer) error { fd, err := f.write(pathname, content) if err != nil { return err } if err := fd.Close(); err != nil { - return errors.WithStack(err) + return fault.Wrap(err) } return nil@@ -95,39 +96,39 @@ Encodings: map[string]*buffer.Buffer{}, } } -func (f *Files) WriteFile(file *storage.File, content *buffer.Buffer) errors.E { +func (f *Files) WriteFile(file *storage.File, content *buffer.Buffer) error { return f.Write(file.Path, file.Title, content) } -func (f *Files) write(pathname string, content *buffer.Buffer) (multifile.FileLike, errors.E) { +func (f *Files) write(pathname string, content *buffer.Buffer) (multifile.FileLike, error) { filename := pathNameToFileName(pathname) err := f.Mkdirp(filepath.Dir(filename)) if err != nil { - return nil, errors.WithMessage(err, "could not create directory") + return nil, fault.Wrap(err, fmsg.With("could not create directory")) } fd, err := f.OpenFileAndVariants(filename) if err != nil { - return nil, errors.WithMessagef(err, "could not open output file") + return nil, fault.Wrap(err, fmsg.With("could not open output file")) } if _, err := fd.Write(content.Bytes()); err != nil { - return nil, errors.WithMessage(err, "could not write output file") + return nil, fault.Wrap(err, fmsg.With("could not write output file")) } return fd, nil } -func openFileWrite(filename string) (*os.File, errors.E) { +func openFileWrite(filename string) (*os.File, error) { f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) if err != nil { - return nil, errors.WithMessage(err, "could not open output file") + return nil, fault.Wrap(err, fmsg.With("could not open output file")) } return f, nil } -func openFileGz(filename string) (*multifile.CompressWriter, errors.E) { +func openFileGz(filename string) (*multifile.CompressWriter, error) { filenameGz := filename + ".gz" f, err := openFileWrite(filenameGz) if err != nil {@@ -137,13 +138,13 @@ var w io.WriteCloser var baseErr error w, baseErr = gzip.NewWriterLevel(f, gzipLevel) if baseErr != nil { - return nil, errors.WithStack(baseErr) + return nil, fault.Wrap(baseErr) } return multifile.NewCompressWriter(f, w), err } -func openFileBrotli(filename string) (*multifile.CompressWriter, errors.E) { +func openFileBrotli(filename string) (*multifile.CompressWriter, error) { filenameBrotli := filename + ".br" f, err := openFileWrite(filenameBrotli) if err != nil {@@ -153,7 +154,7 @@ return multifile.NewCompressWriter(f, brotli.NewWriterLevel(f, brotliLevel)), nil } -func openFileZstd(filename string) (*multifile.CompressWriter, errors.E) { +func openFileZstd(filename string) (*multifile.CompressWriter, error) { f, err := openFileWrite(filename + ".zstd") if err != nil { return nil, err@@ -163,13 +164,13 @@ var w io.WriteCloser var baseErr error w, baseErr = zstd.NewWriter(f) if baseErr != nil { - return nil, errors.WithStack(baseErr) + return nil, fault.Wrap(baseErr) } return multifile.NewCompressWriter(f, w), nil } -func multiOpenFile(filename string) (*multifile.MultiFile, errors.E) { +func multiOpenFile(filename string) (*multifile.MultiFile, error) { r, err := openFileWrite(filename) if err != nil { return nil, err@@ -190,7 +191,7 @@ return multifile.NewMultiFile(r, gz, br, zst), nil } -func (f *Files) OpenFileAndVariants(filename string) (multifile.FileLike, errors.E) { +func (f *Files) OpenFileAndVariants(filename string) (multifile.FileLike, error) { if f.options.Compress { return multiOpenFile(f.join(filename)) }@@ -198,10 +199,10 @@ return openFileWrite(f.join(filename)) } -func (f *Files) Mkdirp(dir string) errors.E { +func (f *Files) Mkdirp(dir string) error { err := os.MkdirAll(f.join(dir), 0o750) if err != nil { - return errors.WithMessage(err, "could not create directory") + return fault.Wrap(err, fmsg.With("could not create directory")) } return nil