-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
59 lines (47 loc) · 1.48 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package error
import (
"os"
errors "github.com/apenella/go-common-utils/error"
"github.com/gostevedore/stevedore/internal/infrastructure/cli/command"
"github.com/spf13/cobra"
)
type ErrorManagement struct {
cmd *command.StevedoreCommand
debug bool
logger Logger
console Consoler
}
// NewCommand is a middleware that manages errors output on commands
func NewCommand(c *command.StevedoreCommand, p Consoler, l Logger, debug *bool) *command.StevedoreCommand {
if c.Command.PersistentPreRunE != nil {
c.Command.PersistentPreRunE = errorManagement(l, p, debug, c.Command.PersistentPreRunE)
}
if c.Command.PreRunE != nil {
c.Command.PreRunE = errorManagement(l, p, debug, c.Command.PreRunE)
}
if c.Command.RunE != nil {
c.Command.RunE = errorManagement(l, p, debug, c.Command.RunE)
}
if c.Command.PostRunE != nil {
c.Command.PostRunE = errorManagement(l, p, debug, c.Command.PostRunE)
}
if c.Command.PersistentPostRunE != nil {
c.Command.PersistentPostRunE = errorManagement(l, p, debug, c.Command.PersistentPostRunE)
}
return c
}
func errorManagement(l Logger, c Consoler, debug *bool, f func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
err := f(cmd, args)
if err != nil {
l.Error(err.(*errors.Error).ErrorWithContext())
if *debug {
c.Error(err.(*errors.Error).ErrorWithContext())
} else {
c.Error(err.Error())
}
os.Exit(1)
}
return nil
}
}