Skip to content

Commit 111ca07

Browse files
committed
wrap JSON encoding errors for understandability.
1 parent e768156 commit 111ca07

File tree

8 files changed

+39
-11
lines changed

8 files changed

+39
-11
lines changed

internal/cli/cmd/auth/oidc.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"namespacelabs.dev/foundation/internal/cli/fncobra"
1414
"namespacelabs.dev/foundation/internal/console"
1515
"namespacelabs.dev/foundation/internal/fnapi"
16+
"namespacelabs.dev/foundation/internal/fnerrors"
1617
)
1718

1819
const (
@@ -49,7 +50,9 @@ func printResult(ctx context.Context, output string, resp fnapi.IssueIdTokenResp
4950
case "json":
5051
d := json.NewEncoder(console.Stdout(ctx))
5152
d.SetIndent("", " ")
52-
return d.Encode(resp)
53+
if err := d.Encode(resp); err != nil {
54+
return fnerrors.InternalError("failed to encode token as JSON output: %w", err)
55+
}
5356

5457
default:
5558
if output != "" && output != "plain" {

internal/cli/cmd/cluster/buildx.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,9 @@ func newStatusBuildxCommand() *cobra.Command {
660660
case "json":
661661
enc := json.NewEncoder(console.Stdout(ctx))
662662
enc.SetIndent("", " ")
663-
return enc.Encode(descs)
663+
if err := enc.Encode(descs); err != nil {
664+
return fnerrors.InternalError("failed to encode status as JSON output: %w", err)
665+
}
664666

665667
default:
666668
if *output != "plain" {

internal/cli/cmd/cluster/create.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,13 @@ func NewCreateCmd() *cobra.Command {
163163
enc := json.NewEncoder(console.Stdout(ctx))
164164
enc.SetIndent("", " ")
165165

166-
return enc.Encode(createOutput{
166+
if err := enc.Encode(createOutput{
167167
ClusterId: cluster.ClusterId,
168168
ClusterUrl: cluster.Cluster.AppURL,
169169
IngressDomain: cluster.Cluster.IngressDomain,
170-
})
170+
}); err != nil {
171+
return fnerrors.InternalError("failed to encode cluster as JSON output: %w", err)
172+
}
171173

172174
default:
173175
if *output != "plain" {

internal/cli/cmd/cluster/describe.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"golang.org/x/exp/slices"
1616
"namespacelabs.dev/foundation/internal/cli/fncobra"
1717
"namespacelabs.dev/foundation/internal/console"
18+
"namespacelabs.dev/foundation/internal/fnerrors"
1819
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
1920
)
2021

@@ -61,7 +62,11 @@ func NewDescribeCmd() *cobra.Command {
6162
case "json":
6263
enc := json.NewEncoder(console.Stdout(ctx))
6364
enc.SetIndent("", " ")
64-
return enc.Encode(response.Summary)
65+
if err := enc.Encode(response.Summary); err != nil {
66+
return fnerrors.InternalError("failed to encode summary as JSON output: %w", err)
67+
}
68+
69+
return nil
6570

6671
default:
6772
if *output != "plain" {

internal/cli/cmd/cluster/expose.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,9 @@ func newExposeKubernetesCmd() *cobra.Command {
551551
URL: "https://" + resp.Fqdn,
552552
}
553553

554-
return json.NewEncoder(console.Stdout(ctx)).Encode(exported)
554+
if err := json.NewEncoder(console.Stdout(ctx)).Encode(exported); err != nil {
555+
return fnerrors.InternalError("failed to encode ingress output as JSON output: %w", err)
556+
}
555557

556558
default:
557559
if *output != "" && *output != "plain" {

internal/cli/cmd/cluster/history.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/spf13/cobra"
1313
"namespacelabs.dev/foundation/internal/cli/fncobra"
1414
"namespacelabs.dev/foundation/internal/console"
15+
"namespacelabs.dev/foundation/internal/fnerrors"
1516
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
1617
)
1718

@@ -43,7 +44,11 @@ func NewListCmd() *cobra.Command {
4344
stdout := console.Stdout(ctx)
4445
enc := json.NewEncoder(stdout)
4546
enc.SetIndent("", " ")
46-
return enc.Encode(transform(clusters.Clusters))
47+
if err := enc.Encode(transform(clusters.Clusters)); err != nil {
48+
return fnerrors.InternalError("failed to encode cluster list as JSON output: %w", err)
49+
}
50+
51+
return nil
4752
}
4853

4954
if len(clusters.Clusters) == 0 {
@@ -121,7 +126,11 @@ func newHistoryCmd() *cobra.Command {
121126
stdout := console.Stdout(ctx)
122127
enc := json.NewEncoder(stdout)
123128
enc.SetIndent("", " ")
124-
return enc.Encode(clusters.Clusters)
129+
if err := enc.Encode(clusters.Clusters); err != nil {
130+
return fnerrors.InternalError("failed to encode cluster history as JSON output: %w", err)
131+
}
132+
133+
return nil
125134
}
126135

127136
if len(clusters.Clusters) == 0 {

internal/cli/cmd/cluster/run.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,13 @@ func PrintCreateContainersResult(ctx context.Context, output string, resp *api.C
373373
case "json":
374374
d := json.NewEncoder(console.Stdout(ctx))
375375
d.SetIndent("", " ")
376-
return d.Encode(createOutput{
376+
if err := d.Encode(createOutput{
377377
ClusterId: resp.ClusterId,
378378
ClusterUrl: resp.ClusterUrl,
379379
Container: resp.Container,
380-
})
380+
}); err != nil {
381+
return fnerrors.InternalError("failed to encode countainer creation output as JSON output: %w", err)
382+
}
381383

382384
default:
383385
if output != "" && output != "plain" {

internal/cli/cmd/workspace/cmd.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"namespacelabs.dev/foundation/internal/cli/fncobra"
1414
"namespacelabs.dev/foundation/internal/console"
1515
"namespacelabs.dev/foundation/internal/fnapi"
16+
"namespacelabs.dev/foundation/internal/fnerrors"
1617
)
1718

1819
func NewWorkspaceCmd() *cobra.Command {
@@ -46,7 +47,9 @@ func newDescribeCmd() *cobra.Command {
4647
case "json":
4748
d := json.NewEncoder(stdout)
4849
d.SetIndent("", " ")
49-
return d.Encode(res.Tenant)
50+
if err := d.Encode(res.Tenant); err != nil {
51+
return fnerrors.InternalError("failed to encode tenant as JSON output: %w", err)
52+
}
5053

5154
default:
5255
fmt.Fprintf(stdout, "\nWorkspace details:\n\n")

0 commit comments

Comments
 (0)