Permalink
Browse files

Return error on not found from ListFn

This changes `ListFn`'s implementation so that if it encounters a not
found error, instead of sending a fatal error to log, it coordinates the
return of the error between checker goroutines and sends it back to the
caller.

The main impetus here is that it allows an external program compiling
against rclone as a package to handle a not found, where it currently it
cannot.

This does change error output on a not found a little bit, we go from
this:

    2017/01/09 21:14:03 directory not found

To this:

    2017/01/09 21:13:44 Failed to ls: directory not found
  • Loading branch information...
1 parent a7d8ccd commit 3b1e0b66bbf78024baf06d51c3de837135ef95e2 @brandur brandur committed with Jan 10, 2017
Showing with 17 additions and 4 deletions.
  1. +12 −2 fs/lister.go
  2. +5 −2 fs/operations.go
View
@@ -20,6 +20,7 @@ type Lister struct {
finished sync.Once
level int
filter *Filter
+ err error
}
// NewLister creates a Lister object.
@@ -154,6 +155,14 @@ func (o *Lister) AddDir(dir *Dir) (abort bool) {
return false
}
+// Error returns a globally application error that's been set on the Lister
+// object.
+func (o *Lister) Error() error {
+ o.mu.RLock()
+ defer o.mu.RUnlock()
+ return o.err
+}
+
// IncludeDirectory returns whether this directory should be
// included in the listing (and recursed into or not).
func (o *Lister) IncludeDirectory(remote string) bool {
@@ -168,11 +177,12 @@ func (o *Lister) IncludeDirectory(remote string) bool {
// Multiple goroutines can set the error state concurrently,
// but only the first will be returned to the caller.
func (o *Lister) SetError(err error) {
- o.mu.RLock()
+ o.mu.Lock()
if err != nil && !o.abort {
+ o.err = err
o.results <- listerResult{Err: err}
}
- o.mu.RUnlock()
+ o.mu.Unlock()
o.Finished()
}
View
@@ -654,7 +654,10 @@ func ListFn(f Fs, fn func(Object)) error {
for {
o, err := list.GetObject()
if err != nil {
- log.Fatal(err)
+ // The error will be persisted within the Lister object and
+ // we'll get an opportunity to return it as we leave this
+ // function.
+ return
}
// check if we are finished
if o == nil {
@@ -667,7 +670,7 @@ func ListFn(f Fs, fn func(Object)) error {
}()
}
wg.Wait()
- return nil
+ return list.Error()
}
// mutex for synchronized output

0 comments on commit 3b1e0b6

Please sign in to comment.