Less repetitive error messages in internal/cache

Many instances of errors.Wrap in this package would produce messages
like "Open: open <filename>: no such file or directory"; those now omit
the first "Open:" (or "Stat:", or "MkdirAll"). The function readVersion
now appends its own name to the error message, rather than the function
that failed, to make it easier to spot. Other function names (e.g.,
Load) are already added further up in the call chain.
This commit is contained in:
greatroar 2020-10-03 14:01:58 +02:00
parent c84643c6a9
commit 673dda77c0
2 changed files with 16 additions and 20 deletions

View File

@ -33,12 +33,12 @@ func readVersion(dir string) (v uint, err error) {
}
if err != nil {
return 0, errors.Wrap(err, "ReadFile")
return 0, errors.Wrap(err, "readVersion")
}
ver, err := strconv.ParseUint(string(buf), 10, 32)
if err != nil {
return 0, errors.Wrap(err, "ParseUint")
return 0, errors.Wrap(err, "readVersion")
}
return uint(ver), nil
@ -56,13 +56,13 @@ const cachedirTagSignature = "Signature: 8a477f597d28d172789f06886806bc55\n"
func writeCachedirTag(dir string) error {
if err := fs.MkdirAll(dir, dirMode); err != nil {
return err
return errors.WithStack(err)
}
tagfile := filepath.Join(dir, "CACHEDIR.TAG")
_, err := fs.Lstat(tagfile)
if err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "Lstat")
return errors.WithStack(err)
}
f, err := fs.OpenFile(tagfile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, fileMode)
@ -71,16 +71,16 @@ func writeCachedirTag(dir string) error {
return nil
}
return errors.Wrap(err, "OpenFile")
return errors.WithStack(err)
}
debug.Log("Create CACHEDIR.TAG at %v", dir)
if _, err := f.Write([]byte(cachedirTagSignature)); err != nil {
_ = f.Close()
return errors.Wrap(err, "Write")
return errors.WithStack(err)
}
return f.Close()
return errors.WithStack(f.Close())
}
// New returns a new cache for the repo ID at basedir. If basedir is the empty
@ -98,7 +98,7 @@ func New(id string, basedir string) (c *Cache, err error) {
err = fs.MkdirAll(basedir, 0700)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
// create base dir and tag it as a cache directory
@ -124,7 +124,7 @@ func New(id string, basedir string) (c *Cache, err error) {
if os.IsNotExist(err) {
err = fs.MkdirAll(cachedir, dirMode)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
created = true
}
@ -138,13 +138,13 @@ func New(id string, basedir string) (c *Cache, err error) {
if v < cacheVersion {
err = ioutil.WriteFile(filepath.Join(cachedir, "version"), []byte(fmt.Sprintf("%d", cacheVersion)), fileMode)
if err != nil {
return nil, errors.Wrap(err, "WriteFile")
return nil, errors.WithStack(err)
}
}
for _, p := range cacheLayoutPaths {
if err = fs.MkdirAll(filepath.Join(cachedir, p), dirMode); err != nil {
return nil, err
return nil, errors.WithStack(err)
}
}

View File

@ -48,13 +48,13 @@ func (c *Cache) load(h restic.Handle, length int, offset int64) (io.ReadCloser,
f, err := fs.Open(c.filename(h))
if err != nil {
return nil, errors.Wrap(err, "Open")
return nil, errors.WithStack(err)
}
fi, err := f.Stat()
if err != nil {
_ = f.Close()
return nil, errors.Wrap(err, "Stat")
return nil, errors.WithStack(err)
}
if fi.Size() <= crypto.Extension {
@ -94,15 +94,11 @@ func (c *Cache) saveWriter(h restic.Handle) (io.WriteCloser, error) {
p := c.filename(h)
err := fs.MkdirAll(filepath.Dir(p), 0700)
if err != nil {
return nil, errors.Wrap(err, "MkdirAll")
return nil, errors.WithStack(err)
}
f, err := fs.OpenFile(p, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0400)
if err != nil {
return nil, errors.Wrap(err, "Create")
}
return f, err
return f, errors.WithStack(err)
}
// Save saves a file in the cache.
@ -133,7 +129,7 @@ func (c *Cache) Save(h restic.Handle, rd io.Reader) error {
if err = f.Close(); err != nil {
_ = c.remove(h)
return errors.Wrap(err, "Close")
return errors.WithStack(err)
}
return nil