Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎁 Custom error handler #3

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ var ErrNoRootCommand = errors.New("no root command provided")
// App represents a command line application.
type App struct {
CobraProvider
ErrorHandler
Exit func(code int)
root *cobra.Command
}

// 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.
type CobraProvider interface {
Command() *cobra.Command
Expand All @@ -33,8 +38,12 @@ func New(cp CobraProvider) *App {

// ExecuteOrDie will execute the application or perform os.Exit in case of error.
func (a *App) ExecuteOrDie(options ...Option) {
if err := a.Execute(options...); err != nil {
a.Exit(retcode.Calc(err))
err := a.Execute(options...)
if err == nil {
return
}
if a.ErrorHandler == nil || !a.ErrorHandler(err) {
a.defaultErrorHandler(err)
}
}

Expand Down Expand Up @@ -64,3 +73,9 @@ func (a *App) init() error {
}
return nil
}

func (a *App) defaultErrorHandler(err error) {
if err != nil {
a.Exit(retcode.Calc(err))
}
}
6 changes: 6 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func TestExecuteOrDie(t *testing.T) {
var buf bytes.Buffer
var retcode int
var err error
commandline.New(new(testApp)).ExecuteOrDie(
commandline.WithCommand(func(cmd *cobra.Command) {
cmd.SetOut(&buf)
Expand All @@ -23,9 +24,14 @@ func TestExecuteOrDie(t *testing.T) {
commandline.WithExit(func(code int) {
retcode = code
}),
commandline.WithErrorHandler(func(merr error) bool {
err = merr
return false
}),
)
assert.Equal(t, `example Input: ["arg1" "arg2"]`, buf.String())
assert.Equal(t, 133, retcode)
assert.Assert(t, err != nil)
}

func TestExit(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func WithCommand(fn func(cmd *cobra.Command)) Option {
}
}

// WithErrorHandler will allow changing the default error handler.
func WithErrorHandler(fn ErrorHandler) Option {
return func(app *App) {
app.ErrorHandler = fn
}
}

// WithExit creates an option which sets the exit function.
func WithExit(fn func(code int)) Option {
return func(app *App) {
Expand Down
Loading