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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,7 +83,7 @@ type ExitCoder interface { | |
|
||
type exitError struct { | ||
exitCode int | ||
message interface{} | ||
err error | ||
} | ||
|
||
// NewExitError calls Exit to create a new ExitCoder. | ||
|
@@ -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: | ||
err = fmt.Errorf("%+v", message) | ||
case error: | ||
err = e | ||
default: | ||
err = fmt.Errorf("%+v", message) | ||
} | ||
Comment on lines
+106
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Buf the handling of ErrorFormatter same as default There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
// | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.