Moving error handlers to the cache implementations.

This commit is contained in:
Burcu Dogan 2014-05-21 15:56:44 +02:00
parent ef33919251
commit 0476447419
4 changed files with 46 additions and 14 deletions

View File

@ -12,9 +12,9 @@ import (
// Cache represents a token cacher. // Cache represents a token cacher.
type Cache interface { type Cache interface {
// Read reads a cache token from the specified file. // Read reads a cache token from the specified file.
Read() (token *Token, err error) Read() (token *Token)
// Write writes a token to the specified file. // Write writes a token to the specified file.
Write(token *Token) (err error) Write(token *Token)
} }
// NewFileCache creates a new file cache. // NewFileCache creates a new file cache.
@ -24,25 +24,29 @@ func NewFileCache(filename string) *FileCache {
// FileCache represents a file based token cacher. // FileCache represents a file based token cacher.
type FileCache struct { type FileCache struct {
filename string filename string
ErrorHandlerFunc func(error)
} }
// Read reads a cache token from the specified file. // Read reads a cache token from the specified file.
func (f *FileCache) Read() (token *Token, err error) { func (f *FileCache) Read() (token *Token) {
data, err := ioutil.ReadFile(f.filename) data, err := ioutil.ReadFile(f.filename)
if err != nil { if err == nil {
return nil, err err = json.Unmarshal(data, token)
} }
token = &Token{} if f.ErrorHandlerFunc != nil {
err = json.Unmarshal(data, &token) f.ErrorHandlerFunc(err)
return token, err }
return
} }
// Write writes a token to the specified file. // Write writes a token to the specified file.
func (f *FileCache) Write(token *Token) error { func (f *FileCache) Write(token *Token) {
data, err := json.Marshal(token) data, err := json.Marshal(token)
if err != nil { if err == nil {
return err err = ioutil.WriteFile(f.filename, data, 0644)
}
if f.ErrorHandlerFunc != nil {
f.ErrorHandlerFunc(err)
} }
return ioutil.WriteFile(f.filename, data, 0644)
} }

27
cache_test.go Normal file
View File

@ -0,0 +1,27 @@
// Copyright 2014 The oauth2 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package oauth2
import (
"os"
"testing"
)
func TestFileCacheErrorHandling(t *testing.T) {
var lastErr error
fileCache := NewFileCache("/path/that/doesnt/exist")
fileCache.ErrorHandlerFunc = func(err error) {
lastErr = err
}
fileCache.Read()
if !os.IsNotExist(lastErr) {
t.Fatalf("Read should have invoked the error handling func with os.ErrNotExist, but read err is %v", lastErr)
}
lastErr = nil
fileCache.Write(&Token{})
if !os.IsNotExist(lastErr) {
t.Fatalf("Write should have invoked the error handling func with os.ErrNotExist, but read err is %v", lastErr)
}
}

View File

@ -64,6 +64,7 @@ type TokenFetcher interface {
// If the implementation doesn't know how to retrieve a new token, // If the implementation doesn't know how to retrieve a new token,
// it returns an error. // it returns an error.
FetchToken(existing *Token) (*Token, error) FetchToken(existing *Token) (*Token, error)
// Cache returns the Cache implementation to read/persist user tokens.
Cache() Cache Cache() Cache
} }

View File

@ -98,7 +98,7 @@ func (t *authorizedTransport) RoundTrip(req *http.Request) (resp *http.Response,
if token == nil && cache != nil { if token == nil && cache != nil {
// Try to read from cache initially // Try to read from cache initially
token, _ := cache.Read() token = cache.Read()
} }
if token == nil || token.Expired() { if token == nil || token.Expired() {
// Check if the token is refreshable. // Check if the token is refreshable.