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.
type Cache interface {
// 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(token *Token) (err error)
Write(token *Token)
}
// NewFileCache creates a new file cache.
@@ -24,25 +24,29 @@ func NewFileCache(filename string) *FileCache {
// FileCache represents a file based token cacher.
type FileCache struct {
filename string
filename string
ErrorHandlerFunc func(error)
}
// 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)
if err != nil {
return nil, err
if err == nil {
err = json.Unmarshal(data, token)
}
token = &Token{}
err = json.Unmarshal(data, &token)
return token, err
if f.ErrorHandlerFunc != nil {
f.ErrorHandlerFunc(err)
}
return
}
// 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)
if err != nil {
return err
if err == nil {
err = ioutil.WriteFile(f.filename, data, 0644)
}
if f.ErrorHandlerFunc != nil {
f.ErrorHandlerFunc(err)
}
return ioutil.WriteFile(f.filename, data, 0644)
}