Skip to content

Commit

Permalink
💝 Allow error handlers to access the Cobra' command object (#4)
Browse files Browse the repository at this point in the history
Allow error handlers to access the Cobra' command object
  • Loading branch information
cardil authored Oct 18, 2024
1 parent b266316 commit b448e1c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ type App struct {
}

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

// CobraProvider is used to provide a Cobra command.
type CobraProvider interface {
Expand All @@ -42,7 +43,7 @@ func (a *App) ExecuteOrDie(options ...Option) {
if err == nil {
return
}
if a.ErrorHandler == nil || !a.ErrorHandler(err) {
if a.ErrorHandler == nil || !a.ErrorHandler(err, a.root) {
a.defaultErrorHandler(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestExecuteOrDie(t *testing.T) {
commandline.WithExit(func(code int) {
retcode = code
}),
commandline.WithErrorHandler(func(merr error) bool {
commandline.WithErrorHandler(func(merr error, _ *cobra.Command) bool {
err = merr
return false
}),
Expand Down

0 comments on commit b448e1c

Please sign in to comment.