Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit b7cd44f

Browse files
committed
Give better error when authentication expires
Change-Id: Ie78ab93f709305891cb613a9f3d9c2ecd2c3e342
1 parent d244d35 commit b7cd44f

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

coder-sdk/error.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package coder
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"net/http"
67
"net/http/httputil"
78

@@ -18,19 +19,28 @@ type apiError struct {
1819
} `json:"error"`
1920
}
2021

21-
func bodyError(resp *http.Response) error {
22-
byt, err := httputil.DumpResponse(resp, true)
22+
// HTTPError represents an error from the Coder API.
23+
type HTTPError struct {
24+
*http.Response
25+
}
26+
27+
func (e *HTTPError) Error() string {
28+
dump, err := httputil.DumpResponse(e.Response, false)
2329
if err != nil {
24-
return xerrors.Errorf("dump response: %w", err)
30+
return fmt.Sprintf("dump response: %+v", err)
2531
}
2632

2733
var msg apiError
2834
// Try to decode the payload as an error, if it fails or if there is no error message,
2935
// return the response URL with the dump.
30-
if err := json.NewDecoder(resp.Body).Decode(&msg); err != nil || msg.Err.Msg == "" {
31-
return xerrors.Errorf("%s\n%s", resp.Request.URL, byt)
36+
if err := json.NewDecoder(e.Response.Body).Decode(&msg); err != nil || msg.Err.Msg == "" {
37+
return fmt.Sprintf("%s\n%s", e.Response.Request.URL, dump)
3238
}
3339

3440
// If the payload was a in the expected error format with a message, include it.
35-
return xerrors.Errorf("%s\n%s%s", resp.Request.URL, byt, msg.Err.Msg)
41+
return fmt.Sprintf("%s\n%s%s", e.Response.Request.URL, dump, msg.Err.Msg)
42+
}
43+
44+
func bodyError(resp *http.Response) error {
45+
return &HTTPError{resp}
3646
}

internal/cmd/auth.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"context"
5+
"net/http"
46
"net/url"
57

68
"golang.org/x/xerrors"
@@ -27,8 +29,24 @@ func newClient() (*coder.Client, error) {
2729
return nil, xerrors.Errorf("url misformatted: %w try runing \"coder login\" with a valid URL", err)
2830
}
2931

30-
return &coder.Client{
32+
c := &coder.Client{
3133
BaseURL: u,
3234
Token: sessionToken,
33-
}, nil
35+
}
36+
37+
// Make sure we can make a request so the final
38+
// error is more clean.
39+
_, err = c.Me(context.Background())
40+
if err != nil {
41+
var he *coder.HTTPError
42+
if xerrors.As(err, &he) {
43+
switch he.StatusCode {
44+
case http.StatusUnauthorized:
45+
return nil, xerrors.Errorf("not authenticated: try running \"coder login`\"")
46+
}
47+
}
48+
return nil, err
49+
}
50+
51+
return c, nil
3452
}

internal/cmd/shell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func shell(_ *cobra.Command, cmdArgs []string) error {
7171
if exitErr, ok := err.(wsep.ExitError); ok {
7272
os.Exit(exitErr.Code)
7373
}
74-
return xerrors.Errorf("run command: %w", err)
74+
flog.Fatal("%+v", err)
7575
}
7676
return nil
7777
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy