forked from remote/oauth2
Moving error handlers to the cache implementations.
This commit is contained in:
30
cache.go
30
cache.go
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user