Skip to content

Commit d6e563d

Browse files
authored
Rewrite xerrors with Go errors (int128#32)
1 parent 01a9824 commit d6e563d

File tree

8 files changed

+46
-52
lines changed

8 files changed

+46
-52
lines changed

e2e_test/authserver/handler.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
package authserver
55

66
import (
7+
"errors"
8+
"fmt"
79
"net/http"
810
"net/url"
911
"testing"
10-
11-
"golang.org/x/xerrors"
1212
)
1313

1414
// AuthorizationRequest represents an authorization request described as:
@@ -55,13 +55,13 @@ func (h *Handler) serveHTTP(w http.ResponseWriter, r *http.Request) error {
5555
q := r.URL.Query()
5656
scope, state, redirectURI := q.Get("scope"), q.Get("state"), q.Get("redirect_uri")
5757
if scope == "" {
58-
return xerrors.New("scope is missing")
58+
return errors.New("scope is missing")
5959
}
6060
if state == "" {
61-
return xerrors.New("state is missing")
61+
return errors.New("state is missing")
6262
}
6363
if redirectURI == "" {
64-
return xerrors.New("redirect_uri is missing")
64+
return errors.New("redirect_uri is missing")
6565
}
6666
to := h.NewAuthorizationResponse(AuthorizationRequest{
6767
Scope: scope,
@@ -73,14 +73,14 @@ func (h *Handler) serveHTTP(w http.ResponseWriter, r *http.Request) error {
7373

7474
case r.Method == "POST" && r.URL.Path == "/token":
7575
if err := r.ParseForm(); err != nil {
76-
return xerrors.Errorf("error while parsing form: %w", err)
76+
return fmt.Errorf("error while parsing form: %w", err)
7777
}
7878
code, redirectURI := r.Form.Get("code"), r.Form.Get("redirect_uri")
7979
if code == "" {
80-
return xerrors.New("code is missing")
80+
return errors.New("code is missing")
8181
}
8282
if redirectURI == "" {
83-
return xerrors.New("redirect_uri is missing")
83+
return errors.New("redirect_uri is missing")
8484
}
8585
status, b := h.NewTokenResponse(TokenRequest{
8686
Code: code,
@@ -89,7 +89,7 @@ func (h *Handler) serveHTTP(w http.ResponseWriter, r *http.Request) error {
8989
w.Header().Add("Content-Type", "application/json")
9090
w.WriteHeader(status)
9191
if _, err := w.Write([]byte(b)); err != nil {
92-
return xerrors.Errorf("error while writing response body: %w", err)
92+
return fmt.Errorf("error while writing response body: %w", err)
9393
}
9494

9595
default:

e2e_test/oauth2cli_test.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/tls"
66
"crypto/x509"
7+
"errors"
78
"fmt"
89
"io/ioutil"
910
"net/http"
@@ -16,7 +17,6 @@ import (
1617
"github.com/int128/oauth2cli/e2e_test/authserver"
1718
"golang.org/x/oauth2"
1819
"golang.org/x/sync/errgroup"
19-
"golang.org/x/xerrors"
2020
)
2121

2222
func TestGetToken(t *testing.T) {
@@ -181,7 +181,7 @@ func successfulTest(t *testing.T, cfg oauth2cli.Config, h *authserver.Handler) {
181181
case to := <-openBrowserCh:
182182
status, body, err := openBrowserRequest(to)
183183
if err != nil {
184-
return xerrors.Errorf("could not open browser request: %w", err)
184+
return fmt.Errorf("could not open browser request: %w", err)
185185
}
186186
t.Logf("got response body: %s", body)
187187
if status != 200 {
@@ -192,14 +192,14 @@ func successfulTest(t *testing.T, cfg oauth2cli.Config, h *authserver.Handler) {
192192
}
193193
return nil
194194
case <-ctx.Done():
195-
return xerrors.Errorf("context done while waiting for opening browser: %w", ctx.Err())
195+
return fmt.Errorf("context done while waiting for opening browser: %w", ctx.Err())
196196
}
197197
})
198198
eg.Go(func() error {
199199
// Start a local server and get a token.
200200
token, err := oauth2cli.GetToken(ctx, cfg)
201201
if err != nil {
202-
return xerrors.Errorf("could not get a token: %w", err)
202+
return fmt.Errorf("could not get a token: %w", err)
203203
}
204204
if "ACCESS_TOKEN" != token.AccessToken {
205205
t.Errorf("AccessToken wants %s but %s", "ACCESS_TOKEN", token.AccessToken)
@@ -243,22 +243,22 @@ func errorAuthorizationResponseTest(t *testing.T, cfg oauth2cli.Config) {
243243
case to := <-openBrowserCh:
244244
status, body, err := openBrowserRequest(to)
245245
if err != nil {
246-
return xerrors.Errorf("could not open browser request: %w", err)
246+
return fmt.Errorf("could not open browser request: %w", err)
247247
}
248248
t.Logf("got response body: %s", body)
249249
if status != 500 {
250250
t.Errorf("status wants 500 but %d", status)
251251
}
252252
return nil
253253
case <-ctx.Done():
254-
return xerrors.Errorf("context done while waiting for opening browser: %w", ctx.Err())
254+
return fmt.Errorf("context done while waiting for opening browser: %w", ctx.Err())
255255
}
256256
})
257257
eg.Go(func() error {
258258
// Start a local server and get a token.
259259
_, err := oauth2cli.GetToken(ctx, cfg)
260260
if err == nil {
261-
return xerrors.New("GetToken wants error but was nil")
261+
return errors.New("GetToken wants error but was nil")
262262
}
263263
t.Logf("expected error: %s", err)
264264
return nil
@@ -297,7 +297,7 @@ func errorTokenResponseTest(t *testing.T, cfg oauth2cli.Config) {
297297
case to := <-openBrowserCh:
298298
status, body, err := openBrowserRequest(to)
299299
if err != nil {
300-
return xerrors.Errorf("could not open browser request: %w", err)
300+
return fmt.Errorf("could not open browser request: %w", err)
301301
}
302302
t.Logf("got response body: %s", body)
303303
if status != 200 {
@@ -308,14 +308,14 @@ func errorTokenResponseTest(t *testing.T, cfg oauth2cli.Config) {
308308
}
309309
return nil
310310
case <-ctx.Done():
311-
return xerrors.Errorf("context done while waiting for opening browser: %w", ctx.Err())
311+
return fmt.Errorf("context done while waiting for opening browser: %w", ctx.Err())
312312
}
313313
})
314314
eg.Go(func() error {
315315
// Start a local server and get a token.
316316
_, err := oauth2cli.GetToken(ctx, cfg)
317317
if err == nil {
318-
return xerrors.New("GetToken wants error but nil")
318+
return errors.New("GetToken wants error but nil")
319319
}
320320
t.Logf("expected error: %s", err)
321321
return nil
@@ -338,7 +338,7 @@ func openBrowserRequest(url string) (int, string, error) {
338338
certPool := x509.NewCertPool()
339339
data, err := ioutil.ReadFile("testdata/ca.pem")
340340
if err != nil {
341-
return 0, "", xerrors.Errorf("could not read certificate authority: %w", err)
341+
return 0, "", fmt.Errorf("could not read certificate authority: %w", err)
342342
}
343343
if !certPool.AppendCertsFromPEM(data) {
344344
return 0, "", fmt.Errorf("could not append certificate data")
@@ -348,12 +348,12 @@ func openBrowserRequest(url string) (int, string, error) {
348348
client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{RootCAs: certPool}}}
349349
resp, err := client.Get(url)
350350
if err != nil {
351-
return 0, "", xerrors.Errorf("could not send a request: %w", err)
351+
return 0, "", fmt.Errorf("could not send a request: %w", err)
352352
}
353353
defer resp.Body.Close()
354354
b, err := ioutil.ReadAll(resp.Body)
355355
if err != nil {
356-
return resp.StatusCode, "", xerrors.Errorf("could not read response body: %w", err)
356+
return resp.StatusCode, "", fmt.Errorf("could not read response body: %w", err)
357357
}
358358
return resp.StatusCode, string(b), nil
359359
}

example/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"flag"
6+
"fmt"
67
"log"
78
"os"
89
"strings"
@@ -13,7 +14,6 @@ import (
1314
"golang.org/x/oauth2"
1415
"golang.org/x/oauth2/google"
1516
"golang.org/x/sync/errgroup"
16-
"golang.org/x/xerrors"
1717
)
1818

1919
func init() {
@@ -85,14 +85,14 @@ Then set the following options:`)
8585
log.Printf("could not open the browser: %s", err)
8686
}
8787
return nil
88-
case err := <-ctx.Done():
89-
return xerrors.Errorf("context done while waiting for authorization: %w", err)
88+
case <-ctx.Done():
89+
return fmt.Errorf("context done while waiting for authorization: %w", ctx.Err())
9090
}
9191
})
9292
eg.Go(func() error {
9393
token, err := oauth2cli.GetToken(ctx, cfg)
9494
if err != nil {
95-
return xerrors.Errorf("could not get a token: %w", err)
95+
return fmt.Errorf("could not get a token: %w", err)
9696
}
9797
log.Printf("You got a valid token until %s", token.Expiry)
9898
return nil

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
77
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
88
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
9-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
109
)
1110

1211
go 1.13

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM
44
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
55
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
66
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7-
github.com/int128/listener v1.0.0 h1:a9H3m4jbXgXpxJUK3fxWrh37Iic/UU/kYOGE0WtjbbI=
8-
github.com/int128/listener v1.0.0/go.mod h1:sho0rrH7mNRRZH4hYOYx+xwRDGmtRndaUiu2z9iumes=
97
github.com/int128/listener v1.1.0 h1:2Jb41DWLpkQ3I9bIdBzO8H/tNwMvyl/OBZWtCV5Pjuw=
108
github.com/int128/listener v1.1.0/go.mod h1:68WkmTN8PQtLzc9DucIaagAKeGVyMnyyKIkW4Xn47UA=
119
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
@@ -20,8 +18,6 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
2018
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
2119
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
2220
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
23-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
24-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2521
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
2622
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2723
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=

oauth2cli.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/int128/oauth2cli/oauth2params"
1111
"golang.org/x/oauth2"
12-
"golang.org/x/xerrors"
1312
)
1413

1514
var noopMiddleware = func(h http.Handler) http.Handler { return h }
@@ -96,7 +95,7 @@ func GetToken(ctx context.Context, config Config) (*oauth2.Token, error) {
9695
if config.State == "" {
9796
s, err := oauth2params.NewState()
9897
if err != nil {
99-
return nil, xerrors.Errorf("could not generate a state parameter: %w", err)
98+
return nil, fmt.Errorf("could not generate a state parameter: %w", err)
10099
}
101100
config.State = s
102101
}
@@ -109,11 +108,11 @@ func GetToken(ctx context.Context, config Config) (*oauth2.Token, error) {
109108
config.populateDeprecatedFields()
110109
code, err := receiveCodeViaLocalServer(ctx, &config)
111110
if err != nil {
112-
return nil, xerrors.Errorf("authorization error: %w", err)
111+
return nil, fmt.Errorf("authorization error: %w", err)
113112
}
114113
token, err := config.OAuth2Config.Exchange(ctx, code, config.TokenRequestOptions...)
115114
if err != nil {
116-
return nil, xerrors.Errorf("could not exchange the code and token: %w", err)
115+
return nil, fmt.Errorf("could not exchange the code and token: %w", err)
117116
}
118117
return token, nil
119118
}

oauth2params/params.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import (
66
"crypto/sha256"
77
"encoding/base64"
88
"encoding/binary"
9+
"fmt"
910

1011
"golang.org/x/oauth2"
11-
"golang.org/x/xerrors"
1212
)
1313

1414
// NewState returns a state parameter.
1515
// This generates 256 bits of random bytes.
1616
func NewState() (string, error) {
1717
b, err := random(32)
1818
if err != nil {
19-
return "", xerrors.Errorf("could not generate a random: %w", err)
19+
return "", fmt.Errorf("could not generate a random: %w", err)
2020
}
2121
return base64URLEncode(b), nil
2222
}
@@ -49,7 +49,7 @@ func (pkce *PKCE) TokenRequestOptions() []oauth2.AuthCodeOption {
4949
func NewPKCE() (*PKCE, error) {
5050
b, err := random(32)
5151
if err != nil {
52-
return nil, xerrors.Errorf("could not generate a random: %w", err)
52+
return nil, fmt.Errorf("could not generate a random: %w", err)
5353
}
5454
s := computeS256(b)
5555
return &s, nil
@@ -69,7 +69,7 @@ func computeS256(b []byte) PKCE {
6969
func random(bits int) ([]byte, error) {
7070
b := make([]byte, bits)
7171
if err := binary.Read(rand.Reader, binary.LittleEndian, b); err != nil {
72-
return nil, xerrors.Errorf("read error: %w", err)
72+
return nil, fmt.Errorf("read error: %w", err)
7373
}
7474
return b, nil
7575
}

0 commit comments

Comments
 (0)