8000 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
Next Next commit
add helper text to unauthorized error messages
  • Loading branch information
AbhineetJain committed May 23, 2022
commit 4acc66e696a0f70e282702bcf40c4ad68657ec6e
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
14 changes: 14 additions & 0 deletions codersdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ 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
switch res.StatusCode {
case 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 +142,7 @@ func readBodyAsError(res *http.Response) error {
Response: httpapi.Response{
Message: string(resp),
},
Helper: helper,
}
}

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

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

statusCode int

Helper string
}

func (e *Error) StatusCode() int {
Expand All @@ -174,5 +185,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