mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 02:09:56 +00:00
Merge pull request #13728 from mfojtik/intercept-exit
Auto commit by PR queue bot
This commit is contained in:
commit
3afa6954cc
@ -62,13 +62,37 @@ func AddSourceToErr(verb string, source string, err error) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fatalErrHandler = fatal
|
||||||
|
|
||||||
|
// BehaviorOnFatal allows you to override the default behavior when a fatal
|
||||||
|
// error occurs, which is call os.Exit(1). You can pass 'panic' as a function
|
||||||
|
// here if you prefer the panic() over os.Exit(1).
|
||||||
|
func BehaviorOnFatal(f func(string)) {
|
||||||
|
fatalErrHandler = f
|
||||||
|
}
|
||||||
|
|
||||||
|
// fatal prints the message and then exits. If V(2) or greater, glog.Fatal
|
||||||
|
// is invoked for extended information.
|
||||||
|
func fatal(msg string) {
|
||||||
|
// add newline if needed
|
||||||
|
if !strings.HasSuffix(msg, "\n") {
|
||||||
|
msg += "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
if glog.V(2) {
|
||||||
|
glog.FatalDepth(2, msg)
|
||||||
|
}
|
||||||
|
fmt.Fprint(os.Stderr, msg)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// CheckErr prints a user friendly error to STDERR and exits with a non-zero
|
// CheckErr prints a user friendly error to STDERR and exits with a non-zero
|
||||||
// exit code. Unrecognized errors will be printed with an "error: " prefix.
|
// exit code. Unrecognized errors will be printed with an "error: " prefix.
|
||||||
//
|
//
|
||||||
// This method is generic to the command in use and may be used by non-Kubectl
|
// This method is generic to the command in use and may be used by non-Kubectl
|
||||||
// commands.
|
// commands.
|
||||||
func CheckErr(err error) {
|
func CheckErr(err error) {
|
||||||
checkErr(err, fatal)
|
checkErr(err, fatalErrHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkErr(err error, handleErr func(string)) {
|
func checkErr(err error, handleErr func(string)) {
|
||||||
@ -180,21 +204,6 @@ func messageForError(err error) string {
|
|||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
||||||
// fatal prints the message and then exits. If V(2) or greater, glog.Fatal
|
|
||||||
// is invoked for extended information.
|
|
||||||
func fatal(msg string) {
|
|
||||||
// add newline if needed
|
|
||||||
if !strings.HasSuffix(msg, "\n") {
|
|
||||||
msg += "\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
if glog.V(2) {
|
|
||||||
glog.FatalDepth(2, msg)
|
|
||||||
}
|
|
||||||
fmt.Fprint(os.Stderr, msg)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func UsageError(cmd *cobra.Command, format string, args ...interface{}) error {
|
func UsageError(cmd *cobra.Command, format string, args ...interface{}) error {
|
||||||
msg := fmt.Sprintf(format, args...)
|
msg := fmt.Sprintf(format, args...)
|
||||||
return fmt.Errorf("%s\nsee '%s -h' for help.", msg, cmd.CommandPath())
|
return fmt.Errorf("%s\nsee '%s -h' for help.", msg, cmd.CommandPath())
|
||||||
|
Loading…
Reference in New Issue
Block a user