Skip to content

Commit

Permalink
Move handleError function to public namespace
Browse files Browse the repository at this point in the history
Other parts of the codebase will need to use the handleError
function so refactor it into the main debos namespace.

Signed-off-by: Christopher Obbard <[email protected]>
  • Loading branch information
obbardc committed Jul 26, 2023
1 parent 062a55d commit 5932486
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
11 changes: 11 additions & 0 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ func (c *DebosContext) Origin(o string) (string, bool) {
}
}

func HandleError(context *DebosContext, err error, a Action, stage string) bool {
if err == nil {
return false
}

context.State = Failed
log.Printf("Action `%s` failed at stage %s, error: %s", a, stage, err)
DebugShell(*context)
return true
}

type Action interface {
/* FIXME verify should probably be prepare or somesuch */
Verify(context *DebosContext) error
Expand Down
29 changes: 9 additions & 20 deletions cmd/debos/debos.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ import (
"github.com/jessevdk/go-flags"
)

func handleError(context *debos.DebosContext, err error, a debos.Action, stage string) bool {
if err == nil {
return false
}

context.State = debos.Failed
log.Printf("Action `%s` failed at stage %s, error: %s", a, stage, err)
debos.DebugShell(*context)
return true
}

func do_run(r actions.Recipe, context *debos.DebosContext) (success bool) {
for _, a := range r.Actions {
err := a.Run(context)
Expand All @@ -36,13 +25,13 @@ func do_run(r actions.Recipe, context *debos.DebosContext) (success bool) {
defer func(action debos.Action) {
err := action.Cleanup(context)

if handleError(context, err, action, "Cleanup") {
if debos.HandleError(context, err, action, "Cleanup") {
success = false
}
}(a)

// Check the state of Run method
if handleError(context, err, a, "Run") {
if debos.HandleError(context, err, a, "Run") {
return false
}
}
Expand Down Expand Up @@ -240,7 +229,7 @@ func main() {

for _, a := range r.Actions {
err = a.Verify(&context)
if handleError(&context, err, a, "Verify") {
if debos.HandleError(&context, err, a, "Verify") {
return
}
}
Expand Down Expand Up @@ -318,11 +307,11 @@ func main() {
err := action.PostMachineCleanup(&context)

// report errors but do not stop execution
handleError(&context, err, action, "PostMachineCleanup")
debos.HandleError(&context, err, action, "PostMachineCleanup")
}(a)

err = a.PreMachine(&context, m, &args)
if handleError(&context, err, a, "PreMachine") {
if debos.HandleError(&context, err, a, "PreMachine") {
return
}
}
Expand All @@ -342,7 +331,7 @@ func main() {

for _, a := range r.Actions {
err = a.PostMachine(&context)
if handleError(&context, err, a, "PostMachine") {
if debos.HandleError(&context, err, a, "PostMachine") {
return
}
}
Expand All @@ -358,11 +347,11 @@ func main() {
err := action.PostMachineCleanup(&context)

// report errors but do not stop execution
handleError(&context, err, action, "PostMachineCleanup")
debos.HandleError(&context, err, action, "PostMachineCleanup")
}(a)

err = a.PreNoMachine(&context)
if handleError(&context, err, a, "PreNoMachine") {
if debos.HandleError(&context, err, a, "PreNoMachine") {
return
}
}
Expand All @@ -385,7 +374,7 @@ func main() {
if !fakemachine.InMachine() {
for _, a := range r.Actions {
err = a.PostMachine(&context)
if handleError(&context, err, a, "PostMachine") {
if exitcode = debos.HandleError(&context, err, a, "PostMachine"); exitcode != 0 {
return
}
}
Expand Down

0 comments on commit 5932486

Please sign in to comment.