558: handle multi formatter errors
This commit is contained in:
parent
af372e8e2a
commit
b0a8f25773
16
errors.go
16
errors.go
@ -81,10 +81,12 @@ func HandleExitCoder(err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if exitErr, ok := err.(ExitCoder); ok {
|
if exitErr, ok := err.(ExitCoder); ok {
|
||||||
if _, ok := exitErr.(ErrorFormatter); ok {
|
if err.Error() != "" {
|
||||||
fmt.Fprintf(ErrWriter, "%+v\n", err)
|
if _, ok := exitErr.(ErrorFormatter); ok {
|
||||||
} else {
|
fmt.Fprintf(ErrWriter, "%+v\n", err)
|
||||||
fmt.Fprintln(ErrWriter, err)
|
} else {
|
||||||
|
fmt.Fprintln(ErrWriter, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
OsExiter(exitErr.ExitCode())
|
OsExiter(exitErr.ExitCode())
|
||||||
return
|
return
|
||||||
@ -98,7 +100,11 @@ func HandleExitCoder(err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err.Error() != "" {
|
if err.Error() != "" {
|
||||||
fmt.Fprintln(ErrWriter, err)
|
if _, ok := err.(ErrorFormatter); ok {
|
||||||
|
fmt.Fprintf(ErrWriter, "%+v\n", err)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintln(ErrWriter, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
OsExiter(1)
|
OsExiter(1)
|
||||||
}
|
}
|
||||||
|
@ -111,6 +111,10 @@ type ErrorWithFormat struct {
|
|||||||
error
|
error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewErrorWithFormat(m string) *ErrorWithFormat {
|
||||||
|
return &ErrorWithFormat{error: errors.New(m)}
|
||||||
|
}
|
||||||
|
|
||||||
func (f *ErrorWithFormat) Format(s fmt.State, verb rune) {
|
func (f *ErrorWithFormat) Format(s fmt.State, verb rune) {
|
||||||
fmt.Fprintf(s, "This the format: %v", f.error)
|
fmt.Fprintf(s, "This the format: %v", f.error)
|
||||||
}
|
}
|
||||||
@ -128,9 +132,26 @@ func TestHandleExitCoder_ErrorWithFormat(t *testing.T) {
|
|||||||
ErrWriter = fakeErrWriter
|
ErrWriter = fakeErrWriter
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := &ErrorWithFormat{error: errors.New("I am formatted")}
|
err := NewErrorWithFormat("I am formatted")
|
||||||
HandleExitCoder(err)
|
HandleExitCoder(err)
|
||||||
|
|
||||||
expect(t, called, true)
|
expect(t, called, true)
|
||||||
expect(t, ErrWriter.(*bytes.Buffer).String(), "This the format: I am formatted\n")
|
expect(t, ErrWriter.(*bytes.Buffer).String(), "This the format: I am formatted\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandleExitCoder_MultiErrorWithFormat(t *testing.T) {
|
||||||
|
called := false
|
||||||
|
|
||||||
|
OsExiter = func(rc int) {
|
||||||
|
called = true
|
||||||
|
}
|
||||||
|
ErrWriter = &bytes.Buffer{}
|
||||||
|
|
||||||
|
defer func() { OsExiter = fakeOsExiter }()
|
||||||
|
|
||||||
|
err := NewMultiError(NewErrorWithFormat("err1"), NewErrorWithFormat("err2"))
|
||||||
|
HandleExitCoder(err)
|
||||||
|
|
||||||
|
expect(t, called, true)
|
||||||
|
expect(t, ErrWriter.(*bytes.Buffer).String(), "This the format: err1\nThis the format: err2\n")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user