FAQ
Reviewers: adg,

Message:
Hello adg@golang.org (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/goauth2/


Description:
oauth: Improve error handling. Add Oauth specific error type -
OAuthError.

Please review this at http://codereview.appspot.com/6492081/

Affected files:
M oauth/oauth.go


Index: oauth/oauth.go
===================================================================
--- a/oauth/oauth.go
+++ b/oauth/oauth.go
@@ -39,14 +39,21 @@

import (
"encoding/json"
- "errors"
- "fmt"
"net/http"
"net/url"
"os"
"time"
)

+type OAuthError struct {
+ prefix string
+ err string
+}
+
+func (oe OAuthError) Error() string {
+ return "OAuthError: " + oe.prefix + ": " + oe.err
+}
+
// Cache specifies the methods that implement a Token cache.
type Cache interface {
Token() (*Token, error)
@@ -60,12 +67,12 @@
func (f CacheFile) Token() (*Token, error) {
file, err := os.Open(string(f))
if err != nil {
- return nil, fmt.Errorf("CacheFile: %v", err)
+ return nil, OAuthError{"CacheFile.Token", err.Error()}
}
tok := &Token{}
dec := json.NewDecoder(file)
if err = dec.Decode(tok); err != nil {
- return nil, fmt.Errorf("CacheFile: %v", err)
+ return nil, OAuthError{"CacheFile.Token", err.Error()}
}
return tok, nil
}
@@ -73,7 +80,7 @@
func (f CacheFile) PutToken(tok *Token) error {
file, err := os.Create(string(f))
if err != nil {
- return fmt.Errorf("CacheFile: %v", err)
+ return OAuthError{"CacheFile.PutToken", err.Error()}
}
enc := json.NewEncoder(file)
return enc.Encode(tok)
@@ -171,7 +178,7 @@
// Exchange takes a code and gets access Token from the remote server.
func (t *Transport) Exchange(code string) (*Token, error) {
if t.Config == nil {
- return nil, errors.New("no Config supplied")
+ return nil, OAuthError{"Exchange", "no Config supplied"}
}
tok := new(Token)
err := t.updateToken(tok, url.Values{
@@ -200,11 +207,11 @@
// as indicated by the Response's StatusCode.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.Config == nil {
- return nil, errors.New("no Config supplied")
+ return nil, OAuthError{"RoundTrip", "no Config supplied"}
}
if t.Token == nil {
if t.TokenCache == nil {
- return nil, errors.New("no Token supplied")
+ return nil, OAuthError{"RoundTrip", "no Token supplied"}
}
var err error
t.Token, err = t.TokenCache.Token()
@@ -228,9 +235,9 @@
// Refresh renews the Transport's AccessToken using its RefreshToken.
func (t *Transport) Refresh() error {
if t.Config == nil {
- return errors.New("no Config supplied")
+ return OAuthError{"Refresh", "no Config supplied"}
} else if t.Token == nil {
- return errors.New("no existing Token")
+ return OAuthError{"Refresh", "no existing Token"}
}

err := t.updateToken(t.Token, url.Values{
@@ -255,7 +262,7 @@
}
defer r.Body.Close()
if r.StatusCode != 200 {
- return errors.New("invalid response: " + r.Status)
+ return OAuthError{"updateToken", r.Status}
}
var b struct {
Access string `json:"access_token"`

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedSep 5, '12 at 5:44p
activeSep 11, '12 at 1:56a
posts3
users2
websitegolang.org

2 users in discussion

Adg: 2 posts Manugarg: 1 post

People

Translate

site design / logo © 2022 Grokbase