mirror of https://github.com/docker/docs.git
Store: Improve error handling.
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
parent
7cc70ee276
commit
750ab8f60f
|
@ -9,11 +9,14 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrNotFound = errors.New("not found")
|
ErrNotFound = errors.New("not found")
|
||||||
ErrAlreadyExists = errors.New("already exists")
|
ErrAlreadyExists = errors.New("already exists")
|
||||||
|
ErrInvalidKey = errors.New("invalid key")
|
||||||
)
|
)
|
||||||
|
|
||||||
// A simple key<->RequestedState store.
|
// A simple key<->RequestedState store.
|
||||||
|
@ -63,19 +66,22 @@ func (s *Store) restore() error {
|
||||||
// Verify the file extension.
|
// Verify the file extension.
|
||||||
extension := filepath.Ext(file)
|
extension := filepath.Ext(file)
|
||||||
if extension != ".json" {
|
if extension != ".json" {
|
||||||
return fmt.Errorf("invalid file extension for filename %s (%s)", file, extension)
|
log.Errorf("invalid file extension for filename %s (%s)", file, extension)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the object back.
|
// Load the object back.
|
||||||
value, err := s.load(path.Join(s.RootDir, file))
|
value, err := s.load(path.Join(s.RootDir, file))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Errorf(err.Error())
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the key.
|
// Extract the key.
|
||||||
key := file[0 : len(file)-len(extension)]
|
key := file[0 : len(file)-len(extension)]
|
||||||
if len(key) == 0 {
|
if len(key) == 0 {
|
||||||
return fmt.Errorf("invalid filename %s", file)
|
log.Errorf("invalid filename %s", file)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store it back.
|
// Store it back.
|
||||||
|
@ -122,6 +128,10 @@ func (s *Store) All() []*RequestedState {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) set(key string, value *RequestedState) error {
|
func (s *Store) set(key string, value *RequestedState) error {
|
||||||
|
if len(key) == 0 {
|
||||||
|
return ErrInvalidKey
|
||||||
|
}
|
||||||
|
|
||||||
data, err := json.MarshalIndent(value, "", " ")
|
data, err := json.MarshalIndent(value, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -20,6 +20,9 @@ func TestStore(t *testing.T) {
|
||||||
|
|
||||||
var ret *RequestedState
|
var ret *RequestedState
|
||||||
|
|
||||||
|
// Add an invalid key
|
||||||
|
assert.EqualError(t, store.Add("", c1), ErrInvalidKey.Error())
|
||||||
|
|
||||||
// Add "foo" into the store.
|
// Add "foo" into the store.
|
||||||
assert.NoError(t, store.Add("foo", c1))
|
assert.NoError(t, store.Add("foo", c1))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue