mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
bug: Cleaner error message for non logged-in users (#1670)
* add helper text to unauthorized error messages * fix lint error, add unit tests * fix test name * fix test name * fix lint errors in test * add unauthorized test for templates create * remove unnecessary variable * remove Error struct, change error message * change [url] to <url>
This commit is contained in:
10
cli/root.go
10
cli/root.go
@ -7,6 +7,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/kirsle/configdir"
|
||||
"github.com/mattn/go-isatty"
|
||||
"github.com/spf13/cobra"
|
||||
@ -113,6 +115,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
|
||||
}
|
||||
}
|
||||
@ -124,6 +130,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
|
||||
}
|
||||
}
|
||||
|
@ -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>'.")
|
||||
})
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
@ -135,6 +143,7 @@ func readBodyAsError(res *http.Response) error {
|
||||
Response: httpapi.Response{
|
||||
Message: string(resp),
|
||||
},
|
||||
Helper: helper,
|
||||
}
|
||||
}
|
||||
|
||||
@ -146,6 +155,7 @@ 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)
|
||||
@ -153,6 +163,7 @@ func readBodyAsError(res *http.Response) error {
|
||||
return &Error{
|
||||
Response: m,
|
||||
statusCode: res.StatusCode,
|
||||
Helper: helper,
|
||||
}
|
||||
}
|
||||
|
||||
@ -162,6 +173,8 @@ type Error struct {
|
||||
httpapi.Response
|
||||
|
||||
statusCode int
|
||||
|
||||
Helper string
|
||||
}
|
||||
|
||||
func (e *Error) StatusCode() int {
|
||||
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user