-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a unique exit code for failed tests, added generic way to bubbl…
…e up errors with exit codes from cmd, organized exit code definitions into one package (#301) Co-authored-by: anishnaik <[email protected]>
- Loading branch information
Showing
4 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package exitcodes | ||
|
||
// ErrorWithExitCode is an `error` type that wraps an existing error and exit code, providing exit codes | ||
// for a given error if they are bubbled up to the top-level. | ||
type ErrorWithExitCode struct { | ||
err error | ||
exitCode int | ||
} | ||
|
||
// NewErrorWithExitCode creates a new error (ErrorWithExitCode) with the provided internal error and exit code. | ||
func NewErrorWithExitCode(err error, exitCode int) *ErrorWithExitCode { | ||
return &ErrorWithExitCode{ | ||
err: err, | ||
exitCode: exitCode, | ||
} | ||
} | ||
|
||
// Error returns the error message string, implementing the `error` interface. | ||
func (e *ErrorWithExitCode) Error() string { | ||
return e.err.Error() | ||
} | ||
|
||
// GetErrorExitCode checks the given exit code that the application should exit with, if this error is bubbled to | ||
// the top-level. This will be 0 for a nil error, 1 for a generic error, or arbitrary if the error is of type | ||
// ErrorWithExitCode. | ||
// Returns the exit code associated with the error. | ||
func GetErrorExitCode(err error) int { | ||
// If we have no error, return 0, if we have a generic error, return 1, if we have a custom error code, unwrap | ||
// and return it. | ||
if err == nil { | ||
return ExitCodeSuccess | ||
} else if unwrappedErr, ok := err.(*ErrorWithExitCode); ok { | ||
return unwrappedErr.exitCode | ||
} else { | ||
return ExitCodeGeneralError | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package exitcodes | ||
|
||
const ( | ||
// ================================ | ||
// Platform-universal exit codes | ||
// ================================ | ||
|
||
// ExitCodeSuccess indicates no errors or failures had occurred. | ||
ExitCodeSuccess = 0 | ||
|
||
// ExitCodeGeneralError indicates some type of general error occurred. | ||
ExitCodeGeneralError = 1 | ||
|
||
// ================================ | ||
// Application-specific exit codes | ||
// ================================ | ||
// Note: Despite not being standardized, exit codes 2-5 are often used for common use cases, so we avoid them. | ||
|
||
// ExitCodeTestFailed indicates a test case had failed. | ||
ExitCodeTestFailed = 7 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters