Refactor error messages for cf-operator CLI
@@ -27,7 +27,6 @@ import (
)
const (
- cfFailedMessage = "cf-operator command failed."
// Port on which the controller-runtime manager listens
managerPort = 2999
)
@@ -37,6 +36,10 @@ var (
debugGracePeriod = time.Second * 5
)
+func wrapError(err error, msg string) error {
+ return errors.Wrap(err, "cf-operator command failed. "+msg)
+}
+
var rootCmd = &cobra.Command{
Use: "cf-operator",
Short: "cf-operator manages BOSH deployments on Kubernetes",
@@ -46,10 +49,10 @@ var rootCmd = &cobra.Command{
restConfig, err := kubeConfig.NewGetter(log).Get(viper.GetString("kubeconfig"))
if err != nil {
- return errors.Wrapf(err, "%s Couldn't fetch Kubeconfig. Ensure kubeconfig is present to continue.", cfFailedMessage)
+ return wrapError(err, "Couldn't fetch Kubeconfig. Ensure kubeconfig is present to continue.")
}
if err := kubeConfig.NewChecker(log).Check(restConfig); err != nil {
- return errors.Wrapf(err, "%s Couldn't check Kubeconfig. Ensure kubeconfig is correct to continue.", cfFailedMessage)
+ return wrapError(err, "Couldn't check Kubeconfig. Ensure kubeconfig is correct to continue.")
}
cfOperatorNamespace := viper.GetString("cf-operator-namespace")
@@ -60,7 +63,7 @@ var rootCmd = &cobra.Command{
viper.GetString("docker-image-tag"),
)
if err != nil {
- return errors.Wrapf(err, "%s Couldn't parse cf-operator docker image reference.", cfFailedMessage)
+ return wrapError(err, "Couldn't parse cf-operator docker image reference.")
}
log.Infof("Starting cf-operator %s with namespace %s", version.Version, cfOperatorNamespace)
@@ -72,7 +75,7 @@ var rootCmd = &cobra.Command{
provider := viper.GetString("provider")
if serviceHost == "" && provider == "" {
- log.Fatalf("%s operator-webhook-service-host flag is not set (env variable: CF_OPERATOR_WEBHOOK_SERVICE_HOST).", cfFailedMessage)
+ return wrapError(errors.New("couldn't determine webhook server"), "operator-webhook-service-host flag is not set (env variable: CF_OPERATOR_WEBHOOK_SERVICE_HOST)")
}
config := config.NewConfig(
@@ -93,7 +96,7 @@ var rootCmd = &cobra.Command{
ctxlog.Info(ctx, "Applying CRDs...")
err := operator.ApplyCRDs(restConfig)
if err != nil {
- return errors.Wrap(err, "failed to apply crds")
+ return wrapError(err, "Couldn't apply CRDs.")
}
}
@@ -105,14 +108,14 @@ var rootCmd = &cobra.Command{
Host: "0.0.0.0",
})
if err != nil {
- return errors.Wrapf(err, cfFailedMessage)
+ return wrapError(err, "Failed to create new manager.")
}
ctxlog.Info(ctx, "Waiting for configurations to be applied into a BOSHDeployment resource...")
err = mgr.Start(signals.SetupSignalHandler())
if err != nil {
- return errors.Wrapf(err, "%s Failed to start cf-operator manager", cfFailedMessage)
+ return wrapError(err, "Failed to start cf-operator manager.")
}
return nil
},
