Skip to content

Commit

Permalink
Call error handler only if the error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Oct 18, 2024
1 parent b881b94 commit 4aaa23a
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ type App struct {
root *cobra.Command
}

// ErrorHandler is a function that will be used to handle the errors. The
// function will be called regardless if an error has been received, so proper
// error checking is required. If true is returned, the default error handling
// will not be used.
// 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

// CobraProvider is used to provide a Cobra command.
Expand All @@ -41,11 +39,10 @@ func New(cp CobraProvider) *App {
// ExecuteOrDie will execute the application or perform os.Exit in case of error.
func (a *App) ExecuteOrDie(options ...Option) {
err := a.Execute(options...)
if a.ErrorHandler == nil {
a.defaultErrorHandler(err)
if err == nil {
return
}
if !a.ErrorHandler(err) {
if a.ErrorHandler == nil || !a.ErrorHandler(err) {
a.defaultErrorHandler(err)
}
}
Expand Down

0 comments on commit 4aaa23a

Please sign in to comment.