Skip to content

Commit

Permalink
statemachines/action: return consistent errors with component informa…
Browse files Browse the repository at this point in the history
…tion

This change ensures all errors are returned by the action statemachine
consistently and that they include component information.
  • Loading branch information
joelrebel committed May 8, 2023
1 parent 05a21c0 commit e648a9d
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions internal/statemachine/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strconv"

sw "github.com/filanov/stateswitch"
"github.com/hashicorp/go-multierror"
"github.com/metal-toolbox/flasher/internal/model"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -34,18 +33,25 @@ var (

// ErrAction is an error type containing information on the Action failure.
type ErrAction struct {
handler string
status string
cause string
handler string
component string
status string
cause string
}

// Error implements the Error() interface
func (e *ErrAction) Error() string {
return fmt.Sprintf("action '%s' with status '%s', returned error: %s", e.handler, e.status, e.cause)
return fmt.Sprintf(
"action '%s' on component '%s' with status '%s', returned error: %s",
e.handler,
e.component,
e.status,
e.cause,
)
}

func newErrAction(handler, status, cause string) error {
return &ErrAction{handler, status, cause}
func newErrAction(handler, component, status, cause string) error {
return &ErrAction{handler, component, status, cause}
}

// ActionStateMachine is an object holding the action statemachine.
Expand Down Expand Up @@ -160,19 +166,31 @@ func (a *ActionStateMachine) Run(ctx context.Context, action *model.Action, tctx
err := a.sm.Run(transitionType, action, tctx)
if err != nil {
// When the condition returns false, run the next transition
// note: do we want to log this for debugging?
if errors.Is(err, sw.NoConditionPassedToRunTransaction) {
continue
return newErrAction(
string(transitionType),
action.Firmware.Component,
string(action.State()),
err.Error(),
)
}

// run transition failed handler
if txErr := a.TransitionFailed(action, tctx); txErr != nil {
err = multierror.Append(err, errors.Wrap(txErr, "actionSM TransitionFailed() error"))
return newErrAction(
string(transitionType),
action.Firmware.Component,
string(action.State()),
txErr.Error(),
)
}

err = newErrAction(string(action.State()), string(transitionType), err.Error())

return err
return newErrAction(
string(transitionType),
action.Firmware.Component,
string(action.State()),
err.Error(),
)
}

a.transitionsCompleted = append(a.transitionsCompleted, transitionType)
Expand Down

0 comments on commit e648a9d

Please sign in to comment.