refactor: return errors with stack traces, where appropriate
1 file changed, 14 insertions(+), 5 deletions(-)
changed files
M internal/file/file.go → internal/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) }