Skip to content

Commit

Permalink
stat: recompose and propagate error instead of just logging it
Browse files Browse the repository at this point in the history
Fixes #801.

Recompose and propagate encountered errors along with their
status/run-level codes, instead of just logging them.

* Exhibit:
```shell
$ drive stat goos-goarch && echo "success"; echo $?
stat: /goos-goarch err: remote path doesn't exist
9
```
  • Loading branch information
odeke-em committed Dec 3, 2016
1 parent 6ee868f commit 2b1b5c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,19 @@ func combineErrors(prevErr, supplementaryErr error) error {
return newErr
}

// copyErrStatus copies the error status code from fromErr
// to toErr only if toErr and fromErr are both non-nil.
func copyErrStatusCode(toErr, fromErr error) error {
if toErr == nil || fromErr == nil {
return toErr
}
codedErr, hasCode := fromErr.(*Error)
if hasCode {
toErr = makeError(toErr, ErrorStatus(codedErr.Code()))
}
return toErr
}

func reComposeError(prevErr error, messages ...string) error {
if len(messages) < 1 {
return prevErr
Expand Down
21 changes: 11 additions & 10 deletions src/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,34 @@ func (g *Commands) Stat() error {
}

func (g *Commands) statfn(fname string, fn func(string) (*File, error)) error {
var err error
for _, src := range g.opts.Sources {
f, err := fn(src)
if err != nil {
g.log.LogErrf("%s: %s err: %v\n", fname, src, err)
f, fErr := fn(src)
if fErr != nil {
msg := fmt.Sprintf("%s: %s err: %v\n", fname, src, fErr)
err = reComposeError(err, msg)
err = copyErrStatusCode(err, fErr)
continue
}

if g.opts.Md5sum {

src = f.Name // forces filename if -id is used

// md5sum with no arguments should do md5sum *
if f.IsDir && rootLike(g.opts.Path) {
src = ""
}

}

err = g.stat(src, f, g.opts.Depth)

if err != nil {
g.log.LogErrf("%s: %s err: %v\n", fname, src, err)
if fErr = g.stat(src, f, g.opts.Depth); fErr != nil {
msg := fmt.Sprintf("%s: %s err: %v\n", fname, src, fErr)
err = reComposeError(err, msg)
err = copyErrStatusCode(err, fErr)
continue
}
}

return nil
return err
}

func prettyPermission(logf log.Loggerf, perm *drive.Permission) {
Expand Down

0 comments on commit 2b1b5c6

Please sign in to comment.