Пример #1
0
func checkErr(err error, handleErr func(string)) {
	if err == nil {
		return
	}

	if errors.IsInvalid(err) {
		details := err.(*errors.StatusError).Status().Details
		prefix := fmt.Sprintf("The %s %q is invalid:", details.Kind, details.Name)
		errs := statusCausesToAggrError(details.Causes)
		handleErr(MultilineError(prefix, errs))
	}

	// handle multiline errors
	if clientcmd.IsConfigurationInvalid(err) {
		handleErr(MultilineError("Error in configuration: ", err))
	}
	if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) > 0 {
		handleErr(MultipleErrors("", agg.Errors()))
	}

	msg, ok := StandardErrorMessage(err)
	if !ok {
		msg = fmt.Sprintf("error: %s\n", err.Error())
	}
	handleErr(msg)
}
Пример #2
0
func checkCustomErr(customPrefix string, err error, handleErr func(string)) {
	if err == nil {
		return
	}

	if errors.IsInvalid(err) {
		details := err.(*errors.StatusError).Status().Details
		for i := range details.Causes {
			c := &details.Causes[i]
			s := strings.Split(c.Message, "}': ")
			if len(s) == 2 {
				c.Message =
					s[1]
				c.Field = ""
			}
		}
		prefix := fmt.Sprintf("%s", customPrefix)
		errs := statusCausesToAggrError(details.Causes)
		handleErr(MultilineError(prefix, errs))
	}

	// handle multiline errors
	if clientcmd.IsConfigurationInvalid(err) {
		handleErr(MultilineError("Error in configuration: ", err))
	}
	if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) > 0 {
		handleErr(MultipleErrors("", agg.Errors()))
	}

	msg, ok := StandardErrorMessage(err)
	if !ok {
		msg = fmt.Sprintf("error: %s\n", err.Error())
	}
	handleErr(msg)
}
Пример #3
0
func CheckErr(err error) {
	if err != nil {
		if debugErr, ok := err.(debugError); ok {
			glog.V(4).Infof(debugErr.DebugError())
		}
		_, isStatus := err.(client.APIStatus)
		switch {
		case clientcmd.IsConfigurationInvalid(err):
			fatal(MultilineError("Error in configuration: ", err))
		case isStatus:
			fatal(fmt.Sprintf("Error from server: %s", err.Error()))
		case errors.IsUnexpectedObjectError(err):
			fatal(fmt.Sprintf("Server returned an unexpected response: %s", err.Error()))
		}
		switch t := err.(type) {
		case *url.Error:
			glog.V(4).Infof("Connection error: %s %s: %v", t.Op, t.URL, t.Err)
			switch {
			case strings.Contains(t.Err.Error(), "connection refused"):
				host := t.URL
				if server, err := url.Parse(t.URL); err == nil {
					host = server.Host
				}
				fatal(fmt.Sprintf("The connection to the server %s was refused - did you specify the right host or port?", host))
			}
			fatal(fmt.Sprintf("Unable to connect to the server: %v", t.Err))
		}
		fatal(fmt.Sprintf("Error: %s", err.Error()))
	}
}
Пример #4
0
func detectReason(err error) int {
	if err != nil {
		switch {
		case strings.Contains(err.Error(), "certificate signed by unknown authority"):
			return certificateAuthorityUnknownReason
		case strings.Contains(err.Error(), "no server defined"):
			return noServerFoundReason
		case clientcmd.IsConfigurationInvalid(err):
			return configurationInvalidReason
		}
	}
	return unknownReason
}
Пример #5
0
// 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.
//
// This method is generic to the command in use and may be used by non-Kubectl
// commands.
func CheckErr(err error) {
	if err == nil {
		return
	}

	// handle multiline errors
	if clientcmd.IsConfigurationInvalid(err) {
		fatal(MultilineError("Error in configuration: ", err))
	}
	if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) > 0 {
		fatal(MultipleErrors("", agg.Errors()))
	}

	msg, ok := StandardErrorMessage(err)
	if !ok {
		msg = fmt.Sprintf("error: %s\n", err.Error())
	}
	fatal(msg)
}