diff --git a/cache.go b/cache.go index 72752be..a3745e1 100644 --- a/cache.go +++ b/cache.go @@ -13,9 +13,9 @@ import ( // Cache represents a token cacher. type Cache interface { // Reads a cached token. It may return nil if none is cached. - Read() (token *Token, err error) + Read() (*Token, error) // Write writes a token to the cache. - Write(token *Token) + Write(*Token) error } // NewFileCache creates a new file cache. @@ -25,9 +25,6 @@ func NewFileCache(filename string) (cache *FileCache) { // FileCache represents a file based token cacher. type FileCache struct { - // Handler to be invoked if an error occurs during writing. - ErrorHandler func(error) - filename string } @@ -49,12 +46,10 @@ func (f *FileCache) Read() (token *Token, err error) { } // Write writes a token to the specified file. -func (f *FileCache) Write(token *Token) { +func (f *FileCache) Write(token *Token) error { data, err := json.Marshal(token) if err == nil { err = ioutil.WriteFile(f.filename, data, 0644) } - if f.ErrorHandler != nil { - f.ErrorHandler(err) - } + return err } diff --git a/cache_test.go b/cache_test.go index a024811..f745726 100644 --- a/cache_test.go +++ b/cache_test.go @@ -54,18 +54,16 @@ func TestFileCacheWrite(t *testing.T) { } cache := NewFileCache(path.Join(dirName, "cache-file")) - cache.ErrorHandler = func(err error) { - if err != nil { - t.Fatalf("Cache write should have been succeeded succesfully, recieved %v", err) - } - } - - cache.Write(&Token{ + err = cache.Write(&Token{ AccessToken: "abc123", TokenType: "Bearer", RefreshToken: "def789", }) + if err != nil { + t.Errorf("Cache write should have been succeeded succesfully, recieved %v", err) + } + data, err := ioutil.ReadFile(cache.filename) if err != nil { t.Fatal(err) diff --git a/transport.go b/transport.go index 67ea969..5832080 100644 --- a/transport.go +++ b/transport.go @@ -177,7 +177,7 @@ func (t *authorizedTransport) RefreshToken() error { t.token = token if t.cache != nil { - t.cache.Write(token) + return t.cache.Write(token) } return nil diff --git a/transport_test.go b/transport_test.go index cfa7fdb..5f731e9 100644 --- a/transport_test.go +++ b/transport_test.go @@ -13,8 +13,9 @@ func (m *mockCache) Read() (token *Token, err error) { return m.token, nil } -func (m *mockCache) Write(token *Token) { +func (m *mockCache) Write(token *Token) error { m.token = token + return nil } type mockTokenFetcher struct{ token *Token }