all repos — homestead @ a9f47f4e4ed3223e86dc1d5c3edd005b8d9ce589

Code for my website

refactor: return errors with stack traces, where appropriate

Alan Pearce
commit

a9f47f4e4ed3223e86dc1d5c3edd005b8d9ce589

parent

f17c0bd9ce8ad16b39c9c5ba80c917049e135bed

1 file changed, 14 insertions(+), 5 deletions(-)

changed files
M internal/file/file.gointernal/file/file.go
@@ -9,8 +9,17 @@
"gitlab.com/tozd/go/errors" ) +func Stat(path string) (fs.FileInfo, errors.E) { + stat, err := os.Stat(path) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return nil, errors.WithStack(err) + } + + return stat, nil +} + func Exists(path string) bool { - stat, err := os.Stat(path) + stat, err := Stat(path) if err != nil && !errors.Is(err, fs.ErrNotExist) { panic("could not stat path " + path + ": " + err.Error()) }
@@ -18,7 +27,7 @@
return stat != nil } -func Copy(src, dest string) error { +func Copy(src, dest string) errors.E { stat, err := os.Stat(src) if err != nil { return errors.WithMessage(err, "could not stat source file")
@@ -48,15 +57,15 @@
return nil } -func CleanDir(dir string) (err error) { +func CleanDir(dir string) errors.E { files, err := os.ReadDir(dir) if err != nil { - return errors.WithMessage(err, "could not read directory") + return errors.WithMessage(errors.WithStack(err), "could not read directory") } for _, file := range files { err = errors.Join(err, os.RemoveAll(filepath.Join(dir, file.Name()))) } - return err + return errors.WithStack(err) }