Skip to content

Commit 87863ac

Browse files
committed
Switch error message
1 parent 995b813 commit 87863ac

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

cli/organization.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"slices"
8+
"strings"
79

810
"github.com/coder/coder/v2/cli/clibase"
911
"github.com/coder/coder/v2/cli/cliui"
@@ -22,6 +24,7 @@ func (r *RootCmd) organizations() *clibase.Cmd {
2224
},
2325
Children: []*clibase.Cmd{
2426
r.currentOrganization(),
27+
r.switchOrganization(),
2528
},
2629
}
2730

@@ -52,13 +55,44 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
5255
),
5356
Options: clibase.OptionSet{},
5457
Handler: func(inv *clibase.Invocation) error {
58+
orgArg := inv.Args[0]
5559
conf := r.createConfig()
60+
orgs, err := client.OrganizationsByUser(inv.Context(), codersdk.Me)
61+
if err != nil {
62+
return fmt.Errorf("failed to get organizations: %w", err)
63+
}
64+
5665
// If the user passes an empty string, we want to remove the organization
57-
if inv.Args[0] == "" {
66+
if orgArg == "" {
5867
err := conf.Organization().Delete()
5968
if err != nil && !errors.Is(err, os.ErrNotExist) {
6069
return fmt.Errorf("failed to unset organization: %w", err)
6170
}
71+
} else {
72+
index := slices.IndexFunc(orgs, func(org codersdk.Organization) bool {
73+
return org.Name == orgArg || org.ID.String() == orgArg
74+
})
75+
if index < 0 {
76+
names := make([]string, 0, len(orgs))
77+
for _, org := range orgs {
78+
names = append(names, org.Name)
79+
}
80+
81+
// Using this error for better error message formatting
82+
err := &codersdk.Error{
83+
Response: codersdk.Response{
84+
Message: fmt.Sprintf("Organization %q not found.", orgArg),
85+
Detail: "Ensure the organization argument is correct and you are a member of it.",
86+
},
87+
Helper: fmt.Sprintf("Valid organizations you can switch to: %q", strings.Join(names, ", ")),
88+
}
89+
return err
90+
}
91+
92+
err := conf.Organization().Write(orgs[index].ID.String())
93+
if err != nil {
94+
return fmt.Errorf("failed to write organization to config file: %w", err)
95+
}
6296
}
6397

6498
return nil

cli/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,8 +1192,12 @@ func formatRunCommandError(err *clibase.RunCommandError, opts *formatOpts) strin
11921192
func formatCoderSDKError(from string, err *codersdk.Error, opts *formatOpts) string {
11931193
var str strings.Builder
11941194
if opts.Verbose {
1195-
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("API request error to \"%s:%s\". Status code %d", err.Method(), err.URL(), err.StatusCode())))
1196-
_, _ = str.WriteString("\n")
1195+
// If all these fields are empty, then do not print this information.
1196+
// This can occur if the error is being used outside the api.
1197+
if !(err.Method() == "" && err.URL() == "" && err.StatusCode() == 0) {
1198+
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("API request error to \"%s:%s\". Status code %d", err.Method(), err.URL(), err.StatusCode())))
1199+
_, _ = str.WriteString("\n")
1200+
}
11971201
}
11981202
// Always include this trace. Users can ignore this.
11991203
if from != "" {

0 commit comments

Comments
 (0)