Skip to content

Commit

Permalink
feat: recover do func panic
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Kungla <marko@mkungla.dev>
  • Loading branch information
mkungla committed Mar 29, 2024
1 parent 9fd07d1 commit 5c8e47a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,20 +501,26 @@ func (c *Command) callBeforeAction(sess *Session) error {
return nil
}

func (c *Command) callDoAction(session *Session) error {
func (c *Command) callDoAction(session *Session) (err error) {
c.mu.Lock()
defer c.mu.Unlock()

if c.doAction == nil {
return nil
}

defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%w: %s: %v (recovered)", ErrCommand, c.name, r)
}
}()

args := sdk.NewArgs(c.flags)

if err := c.doAction(session, args); err != nil {
return fmt.Errorf("%w: %s: %w", ErrCommand, c.name, err)
}
return nil
return err
}

func (c *Command) callAfterFailureAction(session *Session, err error) error {
Expand Down

0 comments on commit 5c8e47a

Please sign in to comment.