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
Show file tree
Hide file tree
Changes from all 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 deletions cli/root.go
Original file line number Diff line number Diff 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 Down Expand 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 deletions cli/userlist_test.go
Original file line number Diff line number Diff 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"
)

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)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
errC := make(chan error)
go func() {
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(chan error)
go func() {
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()

var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Contains(t, err.Error(), "Try logging in using 'coder login <url>'.")
})
}
14 changes: 14 additions & 0 deletions coderd/templates_test.go
Original file line number Diff line number Diff 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(),
})

var apiErr *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 deletions codersdk/client.go
Original file line number Diff line number Diff 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.
func readBodyAsError(res *http.Response) error {
contentType := res.Header.Get("Content-Type")

var helper string
if res.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>'."
}

if strings.HasPrefix(contentType, "text/plain") {
resp, err := io.ReadAll(res.Body)
if err != 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,
}
}
return xerrors.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

statusCode int

Helper string
}

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