10000 Add helper text to unauthorized error messages by AbhineetJain · Pull Request #1670 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 23, 2022
Merged
17 changes: 17 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ func Root() *cobra.Command {
return cmd
}

type Error struct {
Err error
Helper string
}

func (w *Error) Error() string {
return fmt.Sprintf("%v: %s", w.Err, w.Helper)
}

// createClient returns a new client from the command context.
// It reads from global configuration files if flags are not set.
func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
Expand All @@ -112,6 +121,10 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
if err != nil || rawURL == "" {
rawURL, err = root.URL().Read()
if err != nil {
err = &Error{
Err: err,
Helper: "Try running \"coder login [url]\".",
}
return nil, err
}
}
Expand All @@ -123,6 +136,10 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
if err != nil || token == "" {
token, err = root.Session().Read()
if err != nil {
err = &Error{
Err: err,
Helper: "Try running \"coder login [url]\".",
}
return nil, err
}
}
Expand Down
54 changes: 38 additions & 16 deletions cli/userlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,42 @@ import (
)

func TestUserList(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)
doneChan := make(chan struct{})
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
go func() {
defer close(doneChan)
err := cmd.Execute()
require.NoError(t, err)
}()
pty.ExpectMatch("coder.com")
<-doneChan
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)
doneChan := make(chan struct{})
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
go func() {
defer close(doneChan)
err := cmd.Execute()
require.NoError(t, err)
}()
pty.ExpectMatch("coder.com")
<-doneChan
})
t.Run("NoURLFileErrorHasHelperText", func(t *testing.T) {
t.Parallel()

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

cmd, err := cmd.ExecuteC()

require.Contains(t, err.Error(), "Try running \"coder login [url]\"")
})
t.Run("SessionAuthErrorHasHelperText", func(t *testing.T) {
t.Parallel()

client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
cmd, root := clitest.New(t, "users", "list")
clitest.SetupConfig(t, client, root)

cmd, err := cmd.ExecuteC()

require.Contains(t, err.Error(), "Try running \"coder login [url]\"")
})
}
13 changes: 13 additions & 0 deletions codersdk/client.go
9785
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func (c *Client) dialWebsocket(ctx context.Context, path string) (*websocket.Con
// wraps it in a codersdk.Error type for easy marshaling.
func readBodyAsError(res *http.Response) error {
contentType := res.Header.Get("Content-Type")

var helper string
if res.StatusCode == http.StatusUnauthorized {
helper = "Try running \"coder login [url]\"."
}

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

Expand All @@ -153,6 +160,7 @@ func readBodyAsError(res *http.Response) error {
return &Error{
Response: m,
statusCode: res.StatusCode,
Helper: helper,
}
}

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

statusCode int

Helper string
}

func (e *Error) StatusCode() int {
Expand All @@ -174,5 +184,8 @@ func (e *Error) Error() string {
for _, err := range e.Errors {
_, _ = fmt.Fprintf(&builder, "\n\t%s: %s", err.Field, err.Detail)
}
if e.Helper != "" {
_, _ = fmt.Fprintf(&builder, ": %s", e.Helper)
}
return builder.String()
}
0