Skip to content
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

chore: wrap error objects to include context (#10592) #10859

Merged
merged 4 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 deletions cmd/argocd/commands/admin/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ func printStatsSummary(clusters []ClusterWithInfo) {
func runClusterNamespacesCommand(ctx context.Context, clientConfig clientcmd.ClientConfig, action func(appClient *versioned.Clientset, argoDB db.ArgoDB, clusters map[string][]string) error) error {
clientCfg, err := clientConfig.ClientConfig()
if err != nil {
return err
return fmt.Errorf("error while creating client config: %w", err)
}
namespace, _, err := clientConfig.Namespace()
if err != nil {
return err
return fmt.Errorf("error while getting namespace from client config: %w", err)
}

kubeClient := kubernetes.NewForConfigOrDie(clientCfg)
Expand All @@ -235,17 +235,16 @@ func runClusterNamespacesCommand(ctx context.Context, clientConfig clientcmd.Cli
argoDB := db.NewDB(namespace, settingsMgr, kubeClient)
clustersList, err := argoDB.ListClusters(ctx)
if err != nil {
return err
return fmt.Errorf("error listing clusters: %w", err)
}
appItems, err := appClient.ArgoprojV1alpha1().Applications(namespace).List(ctx, v1.ListOptions{})
if err != nil {
return err
return fmt.Errorf("error listing application: %w", err)
}
apps := appItems.Items
for i, app := range apps {
err := argo.ValidateDestination(ctx, &app.Spec.Destination, argoDB)
if err != nil {
return err
if err := argo.ValidateDestination(ctx, &app.Spec.Destination, argoDB); err != nil {
return fmt.Errorf("error validating application destination: %w", err)
}
apps[i] = app
}
Expand Down Expand Up @@ -349,15 +348,14 @@ func NewClusterEnableNamespacedMode() *cobra.Command {

cluster, err := argoDB.GetCluster(ctx, server)
if err != nil {
return err
return fmt.Errorf("error getting cluster from server: %w", err)
}
cluster.Namespaces = namespaces
cluster.ClusterResources = clusterResources
fmt.Printf("Setting cluster %s namespaces to %v...", server, namespaces)
if !dryRun {
_, err = argoDB.UpdateCluster(ctx, cluster)
if err != nil {
return err
if _, err = argoDB.UpdateCluster(ctx, cluster); err != nil {
return fmt.Errorf("error updating cluster: %w", err)
}
fmt.Println("done")
} else {
Expand Down Expand Up @@ -405,7 +403,7 @@ func NewClusterDisableNamespacedMode() *cobra.Command {

cluster, err := argoDB.GetCluster(ctx, server)
if err != nil {
return err
return fmt.Errorf("error getting cluster from server: %w", err)
}

if len(cluster.Namespaces) == 0 {
Expand All @@ -415,9 +413,8 @@ func NewClusterDisableNamespacedMode() *cobra.Command {
cluster.Namespaces = nil
fmt.Printf("Disabling namespaced mode for cluster %s...", server)
if !dryRun {
_, err = argoDB.UpdateCluster(ctx, cluster)
if err != nil {
return err
if _, err = argoDB.UpdateCluster(ctx, cluster); err != nil {
return fmt.Errorf("error updating cluster: %w", err)
}
fmt.Println("done")
} else {
Expand Down
8 changes: 4 additions & 4 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2427,12 +2427,12 @@ func NewApplicationEditCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
cli.InteractiveEdit(fmt.Sprintf("%s-*-edit.yaml", appName), appData, func(input []byte) error {
input, err = yaml.YAMLToJSON(input)
if err != nil {
return err
return fmt.Errorf("error converting YAML to JSON: %w", err)
}
updatedSpec := argoappv1.ApplicationSpec{}
err = json.Unmarshal(input, &updatedSpec)
if err != nil {
return err
return fmt.Errorf("error unmarshaling input into application spec: %w", err)
}

var appOpts cmdutil.AppOptions
Expand All @@ -2444,9 +2444,9 @@ func NewApplicationEditCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
AppNamespace: &appNs,
})
if err != nil {
return fmt.Errorf("Failed to update application spec:\n%v", err)
return fmt.Errorf("failed to update application spec: %w", err)
}
return err
return nil
})
},
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/argocd/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func PrintResource(resource interface{}, output string) error {
case "json":
jsonBytes, err := json.MarshalIndent(resource, "", " ")
if err != nil {
return err
return fmt.Errorf("unable to marshal resource to json: %w", err)
}
fmt.Println(string(jsonBytes))
case "yaml":
yamlBytes, err := yaml.Marshal(resource)
if err != nil {
return err
return fmt.Errorf("unable to marshal resource to yaml: %w", err)
}
fmt.Print(string(yamlBytes))
default:
Expand Down Expand Up @@ -56,13 +56,13 @@ func PrintResourceList(resources interface{}, output string, single bool) error
case "json":
jsonBytes, err := json.MarshalIndent(resources, "", " ")
if err != nil {
return err
return fmt.Errorf("unable to marshal resources to json: %w", err)
}
fmt.Println(string(jsonBytes))
case "yaml":
yamlBytes, err := yaml.Marshal(resources)
if err != nil {
return err
return fmt.Errorf("unable to marshal resources to yaml: %w", err)
}
fmt.Print(string(yamlBytes))
default:
Expand Down
10 changes: 5 additions & 5 deletions cmd/argocd/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,23 +863,23 @@ func NewProjectEditCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comman
cli.InteractiveEdit(fmt.Sprintf("%s-*-edit.yaml", projName), projData, func(input []byte) error {
input, err = yaml.YAMLToJSON(input)
if err != nil {
return err
return fmt.Errorf("error converting YAML to JSON: %w", err)
}
updatedSpec := v1alpha1.AppProjectSpec{}
err = json.Unmarshal(input, &updatedSpec)
if err != nil {
return err
return fmt.Errorf("error unmarshaling input into application spec: %w", err)
}
proj, err := projIf.Get(ctx, &projectpkg.ProjectQuery{Name: projName})
if err != nil {
return err
return fmt.Errorf("could not get project by project name: %w", err)
}
proj.Spec = updatedSpec
_, err = projIf.Update(ctx, &projectpkg.ProjectUpdateRequest{Project: proj})
if err != nil {
return fmt.Errorf("Failed to update project:\n%v", err)
return fmt.Errorf("failed to update project:\n%w", err)
}
return err
return nil
})
},
}
Expand Down