Skip to content

Commit

Permalink
Always use named error
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Oct 14, 2020
1 parent 229b822 commit 317d36b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
24 changes: 13 additions & 11 deletions cacher/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func (c *Cacher) Save(ctx context.Context, i *SaveRequest) (retErr error) {
// waste time overwriting the cache.
attrs, err := c.client.Bucket(bucket).Object(key).Attrs(ctx)
if err != nil && !errors.Is(err, storage.ErrObjectNotExist) {
return fmt.Errorf("failed to check if cached object exists: %w", err)
retErr = fmt.Errorf("failed to check if cached object exists: %w", err)
return
}
if attrs != nil {
c.log("cached object already exists, skipping")
Expand All @@ -117,7 +118,7 @@ func (c *Cacher) Save(ctx context.Context, i *SaveRequest) (retErr error) {
gcsw.ObjectAttrs.ContentType = contentType
gcsw.ObjectAttrs.CacheControl = cacheControl
gcsw.ProgressFunc = func(soFar int64) {
fmt.Printf("uploaded %d bytes", soFar)
fmt.Printf("uploaded %d bytes\n", soFar)
}

// Create the gzip writer
Expand Down Expand Up @@ -258,7 +259,8 @@ func (c *Cacher) Restore(ctx context.Context, i *RestoreRequest) (retErr error)
break
}
if err != nil {
return fmt.Errorf("failed to list %s: %w", key, err)
retErr = fmt.Errorf("failed to list %s: %w", key, err)
return
}

c.log("found object %s", key)
Expand Down Expand Up @@ -410,10 +412,8 @@ func (c *Cacher) HashFiles(files []string) (string, error) {
c.log("opening %s", name)
f, err := os.Open(name)
if err != nil {
if cerr := f.Close(); cerr != nil {
return fmt.Errorf("failed to close: %v: failed to open file: %w", cerr, err)
}
return fmt.Errorf("failed to open file: %w", err)
retErr = fmt.Errorf("failed to open file: %w", err)
return
}
defer func() {
c.log("closing %s", name)
Expand All @@ -429,20 +429,22 @@ func (c *Cacher) HashFiles(files []string) (string, error) {
c.log("stating %s", name)
stat, err := f.Stat()
if err != nil {
return fmt.Errorf("failed to stat file: %w", err)
retErr = fmt.Errorf("failed to stat file: %w", err)
return
}

if stat.IsDir() {
c.log("skipping %s (is a directory)", name)
return nil
return
}

c.log("hashing %s", name)
if _, err := io.Copy(h, f); err != nil {
return fmt.Errorf("failed to hash: %w", err)
retErr = fmt.Errorf("failed to hash: %w", err)
return
}

return nil
return
}

for _, name := range files {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func main() {
done()

if err != nil {
fmt.Fprintf(stderr, "%s\n\n", err)
fmt.Fprintf(stderr, "%s\n", err)
if !allowFailure {
os.Exit(1)
}
Expand Down Expand Up @@ -160,6 +160,7 @@ func (s *stringSliceFlag) String() string {
}
return strings.Join(*s, ",")
}

func (s *stringSliceFlag) Set(value string) error {
var vals []string
for _, val := range strings.Split(value, ",") {
Expand Down

0 comments on commit 317d36b

Please sign in to comment.