Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commitb7cd44f

Browse files
committed
Give better error when authentication expires
Change-Id: Ie78ab93f709305891cb613a9f3d9c2ecd2c3e342
1 parentd244d35 commitb7cd44f

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-
funcbodyError(resp*http.Response)error {
22-
byt,err:=httputil.DumpResponse(resp,true)
22+
// HTTPError represents an error from the Coder API.
23+
typeHTTPErrorstruct {
24+
*http.Response
25+
}
26+
27+
func (e*HTTPError)Error()string {
28+
dump,err:=httputil.DumpResponse(e.Response,false)
2329
iferr!=nil {
24-
returnxerrors.Errorf("dump response: %w",err)
30+
returnfmt.Sprintf("dump response: %+v",err)
2531
}
2632

2733
varmsgapiError
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-
iferr:=json.NewDecoder(resp.Body).Decode(&msg);err!=nil||msg.Err.Msg=="" {
31-
returnxerrors.Errorf("%s\n%s",resp.Request.URL,byt)
36+
iferr:=json.NewDecoder(e.Response.Body).Decode(&msg);err!=nil||msg.Err.Msg=="" {
37+
returnfmt.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-
returnxerrors.Errorf("%s\n%s%s",resp.Request.URL,byt,msg.Err.Msg)
41+
returnfmt.Sprintf("%s\n%s%s",e.Response.Request.URL,dump,msg.Err.Msg)
42+
}
43+
44+
funcbodyError(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
returnnil,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+
iferr!=nil {
41+
varhe*coder.HTTPError
42+
ifxerrors.As(err,&he) {
43+
switchhe.StatusCode {
44+
casehttp.StatusUnauthorized:
45+
returnnil,xerrors.Errorf("not authenticated: try running\"coder login`\"")
46+
}
47+
}
48+
returnnil,err
49+
}
50+
51+
returnc,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
ifexitErr,ok:=err.(wsep.ExitError);ok {
7272
os.Exit(exitErr.Code)
7373
}
74-
returnxerrors.Errorf("run command: %w",err)
74+
flog.Fatal("%+v",err)
7575
}
7676
returnnil
7777
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp