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

Add helper text to unauthorized error messages#1670

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
AbhineetJain merged 10 commits intomainfrom917-bug-cleaner-error-message
May 23, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletionscli/root.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,8 @@ import (
"os"
"time"

"golang.org/x/xerrors"

"github.com/kirsle/configdir"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
Expand DownExpand Up@@ -112,6 +114,10 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
if err != nil || rawURL == "" {
rawURL, err = root.URL().Read()
if err != nil {
// If the configuration files are absent, the user is logged out
if os.IsNotExist(err) {
return nil, xerrors.New("You are not logged in. Try logging in using 'coder login <url>'.")
}
return nil, err
}
}
Expand All@@ -123,6 +129,10 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
if err != nil || token == "" {
token, err = root.Session().Read()
if err != nil {
// If the configuration files are absent, the user is logged out
if os.IsNotExist(err) {
return nil, xerrors.New("You are not logged in. Try logging in using 'coder login <url>'.")
}
return nil, err
}
}
Expand Down
53 changes: 39 additions & 14 deletionscli/userlist_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,22 +7,47 @@ import (

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/pty/ptytest"
)

funcTestUserList(t*testing.T) {
t.Parallel()
client:=coderdtest.New(t,nil)
coderdtest.CreateFirstUser(t,client)
cmd,root:=clitest.New(t,"users","list")
clitest.SetupConfig(t,client,root)
pty:=ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
errC:=make(chanerror)
gofunc() {
errC<-cmd.Execute()
}()
require.NoError(t,<-errC)
pty.ExpectMatch("coder.com")
t.Run("List",func(t*testing.T) {
t.Parallel()
client:=coderdtest.New(t,nil)
coderdtest.CreateFirstUser(t,client)
cmd,root:=clitest.New(t,"users","list")
clitest.SetupConfig(t,client,root)
pty:=ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
errC:=make(chanerror)
gofunc() {
errC<-cmd.Execute()
}()
require.NoError(t,<-errC)
pty.ExpectMatch("coder.com")
})
t.Run("NoURLFileErrorHasHelperText",func(t*testing.T) {
t.Parallel()

cmd,_:=clitest.New(t,"users","list")

_,err:=cmd.ExecuteC()

require.Contains(t,err.Error(),"Try logging in using 'coder login <url>'.")
})
t.Run("SessionAuthErrorHasHelperText",func(t*testing.T) {
t.Parallel()

client:=coderdtest.New(t,nil)
cmd,root:=clitest.New(t,"users","list")
clitest.SetupConfig(t,client,root)

_,err:=cmd.ExecuteC()

varapiErr*codersdk.Error
require.ErrorAs(t,err,&apiErr)
require.Contains(t,err.Error(),"Try logging in using 'coder login <url>'.")
})
}
14 changes: 14 additions & 0 deletionscoderd/templates_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,6 +59,20 @@ func TestPostTemplateByOrganization(t *testing.T) {
require.Equal(t,http.StatusConflict,apiErr.StatusCode())
})

t.Run("Unauthorized",func(t*testing.T) {
t.Parallel()
client:=coderdtest.New(t,nil)
_,err:=client.CreateTemplate(context.Background(),uuid.New(), codersdk.CreateTemplateRequest{
Name:"test",
VersionID:uuid.New(),
})

varapiErr*codersdk.Error
require.ErrorAs(t,err,&apiErr)
require.Equal(t,http.StatusUnauthorized,apiErr.StatusCode())
require.Contains(t,err.Error(),"Try logging in using 'coder login <url>'.")
})

t.Run("NoVersion",func(t*testing.T) {
t.Parallel()
client:=coderdtest.New(t,nil)
Expand Down
16 changes: 16 additions & 0 deletionscodersdk/client.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -125,6 +125,14 @@ func (c *Client) dialWebsocket(ctx context.Context, path string) (*websocket.Con
// wraps it in a codersdk.Error type for easy marshaling.
funcreadBodyAsError(res*http.Response)error {
contentType:=res.Header.Get("Content-Type")

varhelperstring
ifres.StatusCode==http.StatusUnauthorized {
// 401 means the user is not logged in
// 403 would mean that the user is not authorized
helper="Try logging in using 'coder login <url>'."
}

ifstrings.HasPrefix(contentType,"text/plain") {
resp,err:=io.ReadAll(res.Body)
iferr!=nil {
Expand All@@ -135,6 +143,7 @@ func readBodyAsError(res *http.Response) error {
Response: httpapi.Response{
Message:string(resp),
},
Helper:helper,
}
}

Expand All@@ -146,13 +155,15 @@ func readBodyAsError(res *http.Response) error {
// If no body is sent, we'll just provide the status code.
return&Error{
statusCode:res.StatusCode,
Helper:helper,
}
}
returnxerrors.Errorf("decode body: %w",err)
}
return&Error{
Response:m,
statusCode:res.StatusCode,
Helper:helper,
}
}

Expand All@@ -162,6 +173,8 @@ type Error struct {
httpapi.Response

statusCodeint

Helperstring
}

func (e*Error)StatusCode()int {
Expand All@@ -171,6 +184,9 @@ func (e *Error) StatusCode() int {
func (e*Error)Error()string {
varbuilder strings.Builder
_,_=fmt.Fprintf(&builder,"status code %d: %s",e.statusCode,e.Message)
ife.Helper!="" {
_,_=fmt.Fprintf(&builder,": %s",e.Helper)
}
for_,err:=rangee.Errors {
_,_=fmt.Fprintf(&builder,"\n\t%s: %s",err.Field,err.Detail)
}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp