Skip to content

Commit

Permalink
flow: more improvements to error processing and printing, especially …
Browse files Browse the repository at this point in the history
…os.Exec (#384)

Co-authored-by: Tony Worm <[email protected]>
  • Loading branch information
verdverm and Tony Worm authored Jun 7, 2024
1 parent 4a96525 commit 7b02f4f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
25 changes: 23 additions & 2 deletions flow/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func prepFlow(R *Runtime, val cue.Value) (*flow.Flow, error) {
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Verbosity = R.Flags.Verbosity
c.ShowErrors = R.Flags.AllErrors

// how to inject tags into original value
// fill / return value
Expand Down Expand Up @@ -177,7 +178,7 @@ func printFinalContext(ctx *flowctx.Context) error {
fmt.Println("\n\n======= final =========")

return yagu.PrintAsTable(
[]string{"Task", "Deps", "Time"},
[]string{"Name", "Task", "Deps", "Time", "Err"},
func(table *tablewriter.Table) ([][]string, error) {
var rows = make([][]string, 0, len(ti))
// fill with data
Expand All @@ -204,7 +205,27 @@ func printFinalContext(ctx *flowctx.Context) error {
deps = fmt.Sprint(ps)
}

row := []string{t.ID, deps, fmt.Sprint(l)}
tt := ""
attrs := t.Orig.Attributes(cue.ValueAttr)
for _, a := range attrs {
if a.Name() == "task" {
tt = fmt.Sprint(a)
break
}
}

// fmt.Println(t.Final)
final := t.CueTask.Value()
// fmt.Println(final)

err := ""
if t.Error != nil {
err = fmt.Sprint(t.Error)
} else if e := final.LookupPath(cue.ParsePath("error")); e.Exists() {
err = fmt.Sprint(e)
}

row := []string{t.ID, tt, deps, fmt.Sprint(l), err}
rows = append(rows, row)
}

Expand Down
4 changes: 4 additions & 0 deletions flow/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type Context struct {
// debug / internal
Verbosity int

// print errors, even if we continue in their presence
ShowErrors bool

Middlewares []Middleware
TaskRegistry *sync.Map

Expand Down Expand Up @@ -83,6 +86,7 @@ func Copy(ctx *Context) *Context {
Stderr: ctx.Stderr,

Verbosity: ctx.Verbosity,
ShowErrors: ctx.ShowErrors,

CUELock: ctx.CUELock,
Mailbox: ctx.Mailbox,
Expand Down
23 changes: 13 additions & 10 deletions flow/tasker/tasker.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,9 @@ func makeTask(ctx *flowctx.Context, node *hof.Node[any]) (cueflow.Runner, error)
// run the hof task
bt.AddTimeEvent("run.beg")
// (update)
value, err := T.Run(c)
value, rerr := T.Run(c)
bt.AddTimeEvent("run.end")

if err != nil {
err = fmt.Errorf("in %q\n%v", c.Value.Path(), cuetils.ExpandCueError(err))
// fmt.Println("RunnerRunc Error:", err)
c.Error = err
bt.Error = err
return err
}

if value != nil {
// fmt.Println("FILL:", taskId, c.Value.Path(), t.Value(), value)
bt.AddTimeEvent("fill.beg")
Expand All @@ -140,20 +132,31 @@ func makeTask(ctx *flowctx.Context, node *hof.Node[any]) (cueflow.Runner, error)
// fmt.Println("FILL:", taskId, c.Value.Path(), value)
//}
err = t.Fill(value)
bt.Final = t.Value()
bt.AddTimeEvent("fill.end")

// fmt.Println("FILL:", taskId, c.Value.Path(), t.Value(), value)
if err != nil {
c.Error = err
bt.Error = err
return err
}

bt.Final = t.Value()
//if node.Hof.Flow.Print.Level > 0 && !node.Hof.Flow.Print.Before {
// // pv := bt.Final.LookupPath(cue.ParsePath(node.Hof.Flow.Print.Path))
// fmt.Printf("%s.%s: %# v\n", node.Hof.Path, node.Hof.Flow.Print.Path, value)
//}

}

if rerr != nil {
rerr = fmt.Errorf("in %q\n%v\n%+v", c.Value.Path(), cuetils.ExpandCueError(rerr), value)
// fmt.Println("RunnerRunc Error:", err)
c.Error = rerr
bt.Error = rerr
return rerr
}

return nil
}), nil
}
13 changes: 7 additions & 6 deletions flow/tasks/os/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (T *Exec) Run(ctx *hofcontext.Context) (interface{}, error) {
//
// run command
//
err = cmd.Run()
rerr := cmd.Run()

// TODO, how to run in the background and wait for signal?

Expand All @@ -114,14 +114,15 @@ func (T *Exec) Run(ctx *hofcontext.Context) (interface{}, error) {
ret["exitcode"] = cmd.ProcessState.ExitCode()
ret["success"] = cmd.ProcessState.Success()

// fmt.Println("GOT HERE", err, v, ret)

if err != nil {
if rerr != nil {
ret["error"] = rerr.Error()
if doExit {
return ret, err
fmt.Printf("In %v\n%v", v.Path(), ret)
return ret, rerr
} else if ctx.ShowErrors {
fmt.Printf("In %v\n%v", v.Path(), ret)
}

ret["error"] = err.Error()
}
return ret, nil
}
Expand Down
5 changes: 5 additions & 0 deletions flow/testdata/tasks/os/flow.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tasks: {
@flow()
r: { filename: "in.txt", contents: string } @task(os.ReadFile)
}

6 changes: 6 additions & 0 deletions flow/testdata/tasks/os/golden.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Error in tasks | tasks: task failed: in "r"
open in.txt: no such file or directory

<nil>

1 error(s) were encountered
2 changes: 2 additions & 0 deletions flow/testdata/tasks/os/readfile_003.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ tasks: {
Error in tasks | tasks: task failed: in "r"
open in.txt: no such file or directory

<nil>

1 error(s) were encountered

0 comments on commit 7b02f4f

Please sign in to comment.