From 1ebb78f5500137b6437a00f1a7ac8bdaa452121b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 1 Nov 2015 12:30:26 +0900 Subject: [PATCH] Improved error handling --- goldsmith.go | 42 +++++++++++++++++++++--------------------- types.go | 3 ++- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/goldsmith.go b/goldsmith.go index db163c0..e078669 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -38,6 +38,7 @@ type stage struct { type goldsmith struct { stages []stage files chan File + err error } func New(src string) Goldsmith { @@ -46,21 +47,19 @@ func New(src string) Goldsmith { return gs } -func (gs *goldsmith) scan(srcDir string) error { +func (gs *goldsmith) scan(srcDir string) { matches, err := doublestar.Glob(filepath.Join(srcDir, "**")) if err != nil { - return err - } - - s := stage{ - input: nil, - output: make(chan File, len(matches)), + gs.err = err + return } + s := stage{nil, make(chan File, len(matches))} for _, match := range matches { relPath, err := filepath.Rel(srcDir, match) if err != nil { - return err + gs.err = err + return } s.output <- &file{relPath: relPath, srcPath: match} @@ -68,15 +67,10 @@ func (gs *goldsmith) scan(srcDir string) error { close(s.output) gs.stages = append(gs.stages, s) - return nil } func (gs *goldsmith) makeStage() stage { - s := stage{ - input: gs.stages[len(gs.stages)-1].output, - output: make(chan File), - } - + s := stage{gs.stages[len(gs.stages)-1].output, make(chan File)} gs.stages = append(gs.stages, s) return s } @@ -85,6 +79,10 @@ func (gs *goldsmith) NewFile(relPath string) File { return &file{relPath: relPath} } +func (gs *goldsmith) SetError(err error) { + gs.err = err +} + func (gs *goldsmith) taskSingle(ts TaskerSingle) { s := gs.makeStage() @@ -109,17 +107,19 @@ func (gs *goldsmith) taskMultiple(tm TaskerMultiple) { } func (gs *goldsmith) Task(task interface{}) Goldsmith { - switch t := task.(type) { - case TaskerSingle: - gs.taskSingle(t) - case TaskerMultiple: - gs.taskMultiple(t) + if gs.err == nil { + switch t := task.(type) { + case TaskerSingle: + gs.taskSingle(t) + case TaskerMultiple: + gs.taskMultiple(t) + } } return gs } -func (gs *goldsmith) Complete(dstDir string) []File { +func (gs *goldsmith) Complete(dstDir string) ([]File, error) { s := gs.stages[len(gs.stages)-1] var files []File @@ -151,5 +151,5 @@ func (gs *goldsmith) Complete(dstDir string) []File { files = append(files, file) } - return files + return files, gs.err } diff --git a/types.go b/types.go index 7f3e9d2..920b1b3 100644 --- a/types.go +++ b/types.go @@ -24,7 +24,7 @@ package goldsmith type Goldsmith interface { Task(task interface{}) Goldsmith - Complete(dstDir string) []File + Complete(dstDir string) ([]File, error) } type TaskerSingle interface { @@ -37,6 +37,7 @@ type TaskerMultiple interface { type Context interface { NewFile(srcDir string) File + SetError(err error) } type File interface {