Skip to content

Commit

Permalink
add the error handler for line flow
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Jun 27, 2022
1 parent f4f0963 commit 12de8e2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,17 @@ func (es *TaskErrors) Append(name string, doErr, undoErr error) {

// AppendTaskError appends the task error.
func (es *TaskErrors) AppendTaskError(e TaskError) { *es = append(*es, e) }

// UnwrapFailedTaskDoError unwraps the original error returned by the failed task.
func UnwrapFailedTaskDoError(err error) error {
switch e := err.(type) {
case FlowError:
return e.TaskErrors[0].DoErr()

case TaskError:
return e.DoErr()

default:
return err
}
}
13 changes: 13 additions & 0 deletions flow_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var _ Flow = &LineFlow{}
type LineFlow struct {
name string
tasks []Task
errf func(err error)

ctxs map[string]interface{}
index int
Expand Down Expand Up @@ -82,8 +83,20 @@ func (f *LineFlow) AddTask(name string, do TaskFunc, undo ...TaskFunc) {
f.AddTasks(NewTask(name, do, undo...))
}

// SetErrorHandler sets the handler to handle it if there is an error.
func (f *LineFlow) SetErrorHandler(handle func(err error)) {
f.errf = handle
}

// Do does the tasks, which undoes them if a certain task fails.
func (f *LineFlow) Do(c context.Context) (err error) {
if err = f.do(c); err != nil && f.errf != nil {
f.errf(err)
}
return
}

func (f *LineFlow) do(c context.Context) (err error) {
f.index = 0
for i, end := 0, len(f.tasks); i < end; i++ {
task := f.tasks[i]
Expand Down

0 comments on commit 12de8e2

Please sign in to comment.