From 8762cb5a71bd9ec2df028b6b10779ac26056c0fa Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 29 Jul 2021 16:35:32 +0300 Subject: [PATCH] display path on error Signed-off-by: Vasiliy Tolstov --- file.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/file.go b/file.go index 2225cfc..dda87c6 100644 --- a/file.go +++ b/file.go @@ -2,7 +2,6 @@ package file import ( "context" - "errors" "fmt" "io" "io/ioutil" @@ -16,7 +15,6 @@ import ( var ( DefaultStructTag = "file" - ErrPathNotExist = errors.New("path is not exist") ) type fileConfig struct { @@ -33,20 +31,16 @@ func (c *fileConfig) Init(opts ...config.Option) error { o(&c.opts) } - path := "" - if c.opts.Context != nil { if v, ok := c.opts.Context.Value(pathKey{}).(string); ok { - path = v + c.path = v } } - if path == "" { - return ErrPathNotExist + if c.path == "" { + return fmt.Errorf("file path not exists: %v", c.path) } - c.path = path - return nil } @@ -59,7 +53,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error fp, err := os.OpenFile(c.path, os.O_RDONLY, os.FileMode(0400)) if err != nil && !c.opts.AllowFail { - return fmt.Errorf("failed to open: %s, error: %w", c.path, ErrPathNotExist) + return fmt.Errorf("failed to open: %s, error: %w", c.path, err) } else if err == nil { defer fp.Close() var buf []byte @@ -109,7 +103,7 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error if err != nil && c.opts.AllowFail { return nil } else if err != nil && !c.opts.AllowFail { - return fmt.Errorf("failed to open: %s, error: %w", c.path, ErrPathNotExist) + return fmt.Errorf("failed to open: %s, error: %w", c.path, err) } if _, werr := fp.Write(buf); werr == nil {