Skip to content

Commit b448e1c

Browse files
authored
💝 Allow error handlers to access the Cobra' command object (#4)
Allow error handlers to access the Cobra' command object
1 parent b266316 commit b448e1c

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

app.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ type App struct {
2020
}
2121

2222
// ErrorHandler is a function that will be used to handle the errors. If true
23-
// is returned, the default error handling will not be used.
24-
type ErrorHandler func(err error) bool
23+
// is returned, the default error handling will not be used. The Cobra's command
24+
// passed to the error handler is the command that thrown the error.
25+
type ErrorHandler func(err error, cmd *cobra.Command) bool
2526

2627
// CobraProvider is used to provide a Cobra command.
2728
type CobraProvider interface {
@@ -42,7 +43,7 @@ func (a *App) ExecuteOrDie(options ...Option) {
4243
if err == nil {
4344
return
4445
}
45-
if a.ErrorHandler == nil || !a.ErrorHandler(err) {
46+
if a.ErrorHandler == nil || !a.ErrorHandler(err, a.root) {
4647
a.defaultErrorHandler(err)
4748
}
4849
}

app_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestExecuteOrDie(t *testing.T) {
2424
commandline.WithExit(func(code int) {
2525
retcode = code
2626
}),
27-
commandline.WithErrorHandler(func(merr error) bool {
27+
commandline.WithErrorHandler(func(merr error, _ *cobra.Command) bool {
2828
err = merr
2929
return false
3030
}),

0 commit comments

Comments
 (0)