Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit87863ac

Browse files
committed
Switch error message
1 parent995b813 commit87863ac

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+
iferr!=nil {
62+
returnfmt.Errorf("failed to get organizations: %w",err)
63+
}
64+
5665
// If the user passes an empty string, we want to remove the organization
57-
ifinv.Args[0]=="" {
66+
iforgArg=="" {
5867
err:=conf.Organization().Delete()
5968
iferr!=nil&&!errors.Is(err,os.ErrNotExist) {
6069
returnfmt.Errorf("failed to unset organization: %w",err)
6170
}
71+
}else {
72+
index:=slices.IndexFunc(orgs,func(org codersdk.Organization)bool {
73+
returnorg.Name==orgArg||org.ID.String()==orgArg
74+
})
75+
ifindex<0 {
76+
names:=make([]string,0,len(orgs))
77+
for_,org:=rangeorgs {
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+
returnerr
90+
}
91+
92+
err:=conf.Organization().Write(orgs[index].ID.String())
93+
iferr!=nil {
94+
returnfmt.Errorf("failed to write organization to config file: %w",err)
95+
}
6296
}
6397

6498
returnnil

‎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
funcformatCoderSDKError(fromstring,err*codersdk.Error,opts*formatOpts)string {
11931193
varstr strings.Builder
11941194
ifopts.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
iffrom!="" {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp