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

Feature:(issue_1090): Add unwrap for ExitCoder #1545

Merged
merged 2 commits into from Oct 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions errors.go
Expand Up @@ -83,7 +83,7 @@ type ExitCoder interface {

type exitError struct {
exitCode int
message interface{}
err error
}

// NewExitError calls Exit to create a new ExitCoder.
Expand All @@ -101,20 +101,35 @@ func NewExitError(message interface{}, exitCode int) ExitCoder {
// by overiding the ExitErrHandler function on an App or the package-global
// OsExiter function.
func Exit(message interface{}, exitCode int) ExitCoder {
var err error

switch e := message.(type) {
case ErrorFormatter:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any difference between this and the default handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well ErrorFormatter implies that the error message can be formatted dfferently than a "regular" error.

err = fmt.Errorf("%+v", message)
case error:
err = e
default:
err = fmt.Errorf("%+v", message)
}
Comment on lines +106 to +113
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well ErrorFormatter implies that the error message can be formatted dfferently than a "regular" error.

Buf the handling of ErrorFormatter same as default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. So what do you suggest be done ? I can collapse the ErrorFormatter into default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually collapsing ErrorFormatter into default doesnt seem to work. I'm going to leave it as it is.


return &exitError{
message: message,
err: err,
exitCode: exitCode,
}
}

func (ee *exitError) Error() string {
return fmt.Sprintf("%v", ee.message)
return ee.err.Error()
}

func (ee *exitError) ExitCode() int {
return ee.exitCode
}

func (ee *exitError) Unwrap() error {
return ee.err
}

// HandleExitCoder handles errors implementing ExitCoder by printing their
// message and calling OsExiter with the given exit code.
//
Expand Down
19 changes: 19 additions & 0 deletions errors_test.go
Expand Up @@ -45,6 +45,25 @@ func TestHandleExitCoder_ExitCoder(t *testing.T) {
expect(t, called, true)
}

func TestHandleExitCoder_ErrorExitCoder(t *testing.T) {
exitCode := 0
called := false

OsExiter = func(rc int) {
if !called {
exitCode = rc
called = true
}
}

defer func() { OsExiter = fakeOsExiter }()

HandleExitCoder(Exit(errors.New("galactic perimeter breach"), 9))

expect(t, exitCode, 9)
expect(t, called, true)
}

func TestHandleExitCoder_MultiErrorWithExitCoder(t *testing.T) {
exitCode := 0
called := false
Expand Down