Better error handling

This commit is contained in:
Alex Yatskov 2015-11-01 13:11:45 +09:00
parent 1ebb78f550
commit 679f2344cb
2 changed files with 44 additions and 44 deletions

View File

@ -75,22 +75,14 @@ func (gs *goldsmith) makeStage() stage {
return s return s
} }
func (gs *goldsmith) NewFile(relPath string) File { func (gs *goldsmith) chainSingle(ts ChainerSingle) {
return &file{relPath: relPath}
}
func (gs *goldsmith) SetError(err error) {
gs.err = err
}
func (gs *goldsmith) taskSingle(ts TaskerSingle) {
s := gs.makeStage() s := gs.makeStage()
var wg sync.WaitGroup var wg sync.WaitGroup
for file := range s.input { for file := range s.input {
wg.Add(1) wg.Add(1)
go func(f File) { go func(f File) {
s.output <- ts.TaskSingle(gs, f) s.output <- ts.ChainSingle(gs, f)
wg.Done() wg.Done()
}(file) }(file)
} }
@ -101,18 +93,26 @@ func (gs *goldsmith) taskSingle(ts TaskerSingle) {
}() }()
} }
func (gs *goldsmith) taskMultiple(tm TaskerMultiple) { func (gs *goldsmith) chainMultiple(tm ChainerMultiple) {
s := gs.makeStage() s := gs.makeStage()
tm.TaskMultiple(gs, s.input, s.output) tm.ChainMultiple(gs, s.input, s.output)
} }
func (gs *goldsmith) Task(task interface{}) Goldsmith { func (gs *goldsmith) NewFile(relPath string) File {
if gs.err == nil { return &file{relPath: relPath}
switch t := task.(type) { }
case TaskerSingle:
gs.taskSingle(t) func (gs *goldsmith) Chain(chain interface{}, err error) Goldsmith {
case TaskerMultiple: if gs.err != nil {
gs.taskMultiple(t) return gs
}
if gs.err = err; gs.err != nil {
switch t := chain.(type) {
case ChainerSingle:
gs.chainSingle(t)
case ChainerMultiple:
gs.chainMultiple(t)
} }
} }
@ -124,13 +124,13 @@ func (gs *goldsmith) Complete(dstDir string) ([]File, error) {
var files []File var files []File
for file := range s.output { for file := range s.output {
if file.Error() == nil {
data := file.Data() data := file.Data()
if data == nil { if data == nil {
continue continue
} }
absPath := filepath.Join(dstDir, file.Path()) absPath := filepath.Join(dstDir, file.Path())
if err := os.MkdirAll(path.Dir(absPath), 0755); err != nil { if err := os.MkdirAll(path.Dir(absPath), 0755); err != nil {
file.SetError(err) file.SetError(err)
continue continue
@ -147,6 +147,7 @@ func (gs *goldsmith) Complete(dstDir string) ([]File, error) {
file.SetError(err) file.SetError(err)
continue continue
} }
}
files = append(files, file) files = append(files, file)
} }

View File

@ -23,21 +23,20 @@
package goldsmith package goldsmith
type Goldsmith interface { type Goldsmith interface {
Task(task interface{}) Goldsmith Chain(task interface{}, err error) Goldsmith
Complete(dstDir string) ([]File, error) Complete(dstDir string) ([]File, error)
} }
type TaskerSingle interface { type ChainerSingle interface {
TaskSingle(ctx Context, file File) File ChainSingle(ctx Context, file File) File
} }
type TaskerMultiple interface { type ChainerMultiple interface {
TaskMultiple(ctx Context, input, output chan File) ChainMultiple(ctx Context, input, output chan File)
} }
type Context interface { type Context interface {
NewFile(srcDir string) File NewFile(srcDir string) File
SetError(err error)
} }
type File interface { type File interface {