Merge pull request #540 from hirose31/fix/handle-multierror

Call HandleExitCoder for all members of MultiError.Errors
This commit is contained in:
Jesse Szwedko 2017-01-16 14:20:16 -08:00 committed by GitHub
commit 56a7254df4
2 changed files with 47 additions and 18 deletions

View File

@ -74,7 +74,7 @@ func (ee *ExitError) ExitCode() int {
// HandleExitCoder checks if the error fulfills the ExitCoder interface, and if
// so prints the error to stderr (if it is non-empty) and calls OsExiter with the
// given exit code. If the given error is a MultiError, then this func is
// called on all members of the Errors slice.
// called on all members of the Errors slice and calls OsExiter with the last exit code.
func HandleExitCoder(err error) {
if err == nil {
return
@ -93,9 +93,8 @@ func HandleExitCoder(err error) {
}
if multiErr, ok := err.(MultiError); ok {
for _, merr := range multiErr.Errors {
HandleExitCoder(merr)
}
code := handleMultiError(multiErr)
OsExiter(code)
return
}
@ -108,3 +107,18 @@ func HandleExitCoder(err error) {
}
OsExiter(1)
}
func handleMultiError(multiErr MultiError) int {
code := 1
for _, merr := range multiErr.Errors {
if multiErr2, ok := merr.(MultiError); ok {
code = handleMultiError(multiErr2)
} else {
fmt.Fprintln(ErrWriter, merr)
if exitErr, ok := merr.(ExitCoder); ok {
code = exitErr.ExitCode()
}
}
}
return code
}

View File

@ -12,8 +12,10 @@ func TestHandleExitCoder_nil(t *testing.T) {
called := false
OsExiter = func(rc int) {
exitCode = rc
called = true
if !called {
exitCode = rc
called = true
}
}
defer func() { OsExiter = fakeOsExiter }()
@ -29,8 +31,10 @@ func TestHandleExitCoder_ExitCoder(t *testing.T) {
called := false
OsExiter = func(rc int) {
exitCode = rc
called = true
if !called {
exitCode = rc
called = true
}
}
defer func() { OsExiter = fakeOsExiter }()
@ -46,17 +50,20 @@ func TestHandleExitCoder_MultiErrorWithExitCoder(t *testing.T) {
called := false
OsExiter = func(rc int) {
exitCode = rc
called = true
if !called {
exitCode = rc
called = true
}
}
defer func() { OsExiter = fakeOsExiter }()
exitErr := NewExitError("galactic perimeter breach", 9)
err := NewMultiError(errors.New("wowsa"), errors.New("egad"), exitErr)
exitErr2 := NewExitError("last ExitCoder", 11)
err := NewMultiError(errors.New("wowsa"), errors.New("egad"), exitErr, exitErr2)
HandleExitCoder(err)
expect(t, exitCode, 9)
expect(t, exitCode, 11)
expect(t, called, true)
}
@ -65,8 +72,10 @@ func TestHandleExitCoder_ErrorWithMessage(t *testing.T) {
called := false
OsExiter = func(rc int) {
exitCode = rc
called = true
if !called {
exitCode = rc
called = true
}
}
ErrWriter = &bytes.Buffer{}
@ -88,8 +97,10 @@ func TestHandleExitCoder_ErrorWithoutMessage(t *testing.T) {
called := false
OsExiter = func(rc int) {
exitCode = rc
called = true
if !called {
exitCode = rc
called = true
}
}
ErrWriter = &bytes.Buffer{}
@ -123,7 +134,9 @@ func TestHandleExitCoder_ErrorWithFormat(t *testing.T) {
called := false
OsExiter = func(rc int) {
called = true
if !called {
called = true
}
}
ErrWriter = &bytes.Buffer{}
@ -143,7 +156,9 @@ func TestHandleExitCoder_MultiErrorWithFormat(t *testing.T) {
called := false
OsExiter = func(rc int) {
called = true
if !called {
called = true
}
}
ErrWriter = &bytes.Buffer{}