Skip to content

Commit b266316

Browse files
authored
🎁 Custom error handler (#3)
* Custom error handler * Call error handler only if the error occurs
1 parent 24d49d4 commit b266316

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

app.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ var ErrNoRootCommand = errors.New("no root command provided")
1414
// App represents a command line application.
1515
type App struct {
1616
CobraProvider
17+
ErrorHandler
1718
Exit func(code int)
1819
root *cobra.Command
1920
}
2021

22+
// 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
25+
2126
// CobraProvider is used to provide a Cobra command.
2227
type CobraProvider interface {
2328
Command() *cobra.Command
@@ -33,8 +38,12 @@ func New(cp CobraProvider) *App {
3338

3439
// ExecuteOrDie will execute the application or perform os.Exit in case of error.
3540
func (a *App) ExecuteOrDie(options ...Option) {
36-
if err := a.Execute(options...); err != nil {
37-
a.Exit(retcode.Calc(err))
41+
err := a.Execute(options...)
42+
if err == nil {
43+
return
44+
}
45+
if a.ErrorHandler == nil || !a.ErrorHandler(err) {
46+
a.defaultErrorHandler(err)
3847
}
3948
}
4049

@@ -64,3 +73,9 @@ func (a *App) init() error {
6473
}
6574
return nil
6675
}
76+
77+
func (a *App) defaultErrorHandler(err error) {
78+
if err != nil {
79+
a.Exit(retcode.Calc(err))
80+
}
81+
}

app_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
func TestExecuteOrDie(t *testing.T) {
1515
var buf bytes.Buffer
1616
var retcode int
17+
var err error
1718
commandline.New(new(testApp)).ExecuteOrDie(
1819
commandline.WithCommand(func(cmd *cobra.Command) {
1920
cmd.SetOut(&buf)
@@ -23,9 +24,14 @@ func TestExecuteOrDie(t *testing.T) {
2324
commandline.WithExit(func(code int) {
2425
retcode = code
2526
}),
27+
commandline.WithErrorHandler(func(merr error) bool {
28+
err = merr
29+
return false
30+
}),
2631
)
2732
assert.Equal(t, `example Input: ["arg1" "arg2"]`, buf.String())
2833
assert.Equal(t, 133, retcode)
34+
assert.Assert(t, err != nil)
2935
}
3036

3137
func TestExit(t *testing.T) {

options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ func WithCommand(fn func(cmd *cobra.Command)) Option {
4141
}
4242
}
4343

44+
// WithErrorHandler will allow changing the default error handler.
45+
func WithErrorHandler(fn ErrorHandler) Option {
46+
return func(app *App) {
47+
app.ErrorHandler = fn
48+
}
49+
}
50+
4451
// WithExit creates an option which sets the exit function.
4552
func WithExit(fn func(code int)) Option {
4653
return func(app *App) {

0 commit comments

Comments
 (0)