Improved error handling

This commit is contained in:
Alex Yatskov 2015-11-01 12:30:26 +09:00
parent 954d451cb5
commit 1ebb78f550
2 changed files with 23 additions and 22 deletions

View File

@ -38,6 +38,7 @@ type stage struct {
type goldsmith struct { type goldsmith struct {
stages []stage stages []stage
files chan File files chan File
err error
} }
func New(src string) Goldsmith { func New(src string) Goldsmith {
@ -46,21 +47,19 @@ func New(src string) Goldsmith {
return gs return gs
} }
func (gs *goldsmith) scan(srcDir string) error { func (gs *goldsmith) scan(srcDir string) {
matches, err := doublestar.Glob(filepath.Join(srcDir, "**")) matches, err := doublestar.Glob(filepath.Join(srcDir, "**"))
if err != nil { if err != nil {
return err gs.err = err
} return
s := stage{
input: nil,
output: make(chan File, len(matches)),
} }
s := stage{nil, make(chan File, len(matches))}
for _, match := range matches { for _, match := range matches {
relPath, err := filepath.Rel(srcDir, match) relPath, err := filepath.Rel(srcDir, match)
if err != nil { if err != nil {
return err gs.err = err
return
} }
s.output <- &file{relPath: relPath, srcPath: match} s.output <- &file{relPath: relPath, srcPath: match}
@ -68,15 +67,10 @@ func (gs *goldsmith) scan(srcDir string) error {
close(s.output) close(s.output)
gs.stages = append(gs.stages, s) gs.stages = append(gs.stages, s)
return nil
} }
func (gs *goldsmith) makeStage() stage { func (gs *goldsmith) makeStage() stage {
s := stage{ s := stage{gs.stages[len(gs.stages)-1].output, make(chan File)}
input: gs.stages[len(gs.stages)-1].output,
output: make(chan File),
}
gs.stages = append(gs.stages, s) gs.stages = append(gs.stages, s)
return s return s
} }
@ -85,6 +79,10 @@ func (gs *goldsmith) NewFile(relPath string) File {
return &file{relPath: relPath} return &file{relPath: relPath}
} }
func (gs *goldsmith) SetError(err error) {
gs.err = err
}
func (gs *goldsmith) taskSingle(ts TaskerSingle) { func (gs *goldsmith) taskSingle(ts TaskerSingle) {
s := gs.makeStage() s := gs.makeStage()
@ -109,17 +107,19 @@ func (gs *goldsmith) taskMultiple(tm TaskerMultiple) {
} }
func (gs *goldsmith) Task(task interface{}) Goldsmith { func (gs *goldsmith) Task(task interface{}) Goldsmith {
if gs.err == nil {
switch t := task.(type) { switch t := task.(type) {
case TaskerSingle: case TaskerSingle:
gs.taskSingle(t) gs.taskSingle(t)
case TaskerMultiple: case TaskerMultiple:
gs.taskMultiple(t) gs.taskMultiple(t)
} }
}
return gs return gs
} }
func (gs *goldsmith) Complete(dstDir string) []File { func (gs *goldsmith) Complete(dstDir string) ([]File, error) {
s := gs.stages[len(gs.stages)-1] s := gs.stages[len(gs.stages)-1]
var files []File var files []File
@ -151,5 +151,5 @@ func (gs *goldsmith) Complete(dstDir string) []File {
files = append(files, file) files = append(files, file)
} }
return files return files, gs.err
} }

View File

@ -24,7 +24,7 @@ package goldsmith
type Goldsmith interface { type Goldsmith interface {
Task(task interface{}) Goldsmith Task(task interface{}) Goldsmith
Complete(dstDir string) []File Complete(dstDir string) ([]File, error)
} }
type TaskerSingle interface { type TaskerSingle interface {
@ -37,6 +37,7 @@ type TaskerMultiple interface {
type Context interface { type Context interface {
NewFile(srcDir string) File NewFile(srcDir string) File
SetError(err error)
} }
type File interface { type File interface {