use error to report error

This commit is contained in:
Chris Lu 2014-09-04 19:34:43 -07:00
parent 4be5ccd0c8
commit a092794804

View file

@ -28,7 +28,7 @@ type DirectoryManagerInMap struct {
isLoading bool isLoading bool
} }
func (dm *DirectoryManagerInMap) NewDirectoryEntryInMap(parent *DirectoryEntryInMap, name string) (d *DirectoryEntryInMap) { func (dm *DirectoryManagerInMap) NewDirectoryEntryInMap(parent *DirectoryEntryInMap, name string) (d *DirectoryEntryInMap, err error) {
writeLock.Lock() writeLock.Lock()
defer writeLock.Unlock() defer writeLock.Unlock()
d = &DirectoryEntryInMap{Name: name, Parent: parent, SubDirectories: make(map[string]*DirectoryEntryInMap)} d = &DirectoryEntryInMap{Name: name, Parent: parent, SubDirectories: make(map[string]*DirectoryEntryInMap)}
@ -38,7 +38,7 @@ func (dm *DirectoryManagerInMap) NewDirectoryEntryInMap(parent *DirectoryEntryIn
} }
n := len(parts) n := len(parts)
if n <= 0 { if n <= 0 {
return d return nil, fmt.Errorf("Failed to create folder %s/%s", parent.Name, name)
} }
for i := 0; i < n/2; i++ { for i := 0; i < n/2; i++ {
parts[i], parts[n-1-i] = parts[n-1-i], parts[i] parts[i], parts[n-1-i] = parts[n-1-i], parts[i]
@ -46,7 +46,7 @@ func (dm *DirectoryManagerInMap) NewDirectoryEntryInMap(parent *DirectoryEntryIn
dm.max++ dm.max++
d.Id = dm.max d.Id = dm.max
dm.log("add", "/"+strings.Join(parts, "/"), strconv.Itoa(int(d.Id))) dm.log("add", "/"+strings.Join(parts, "/"), strconv.Itoa(int(d.Id)))
return d return d, nil
} }
func (dm *DirectoryManagerInMap) log(words ...string) { func (dm *DirectoryManagerInMap) log(words ...string) {
@ -162,7 +162,11 @@ func (dm *DirectoryManagerInMap) loadDirectory(dirPath string, dirId DirectoryId
if i != len(parts)-1 { if i != len(parts)-1 {
return fmt.Errorf("%s should be created after parent %s!", dirPath, parts[i]) return fmt.Errorf("%s should be created after parent %s!", dirPath, parts[i])
} }
sub = dm.NewDirectoryEntryInMap(dir, parts[i]) var err error
sub, err = dm.NewDirectoryEntryInMap(dir, parts[i])
if err != nil {
return err
}
if sub.Id != dirId { if sub.Id != dirId {
return fmt.Errorf("%s should be have id %v instead of %v!", dirPath, sub.Id, dirId) return fmt.Errorf("%s should be have id %v instead of %v!", dirPath, sub.Id, dirId)
} }
@ -183,7 +187,11 @@ func (dm *DirectoryManagerInMap) makeDirectory(dirPath string) (dir *DirectoryEn
for i := 1; i < len(parts); i++ { for i := 1; i < len(parts); i++ {
sub, ok := dir.SubDirectories[parts[i]] sub, ok := dir.SubDirectories[parts[i]]
if !ok { if !ok {
sub = dm.NewDirectoryEntryInMap(dir, parts[i]) var err error
sub, err = dm.NewDirectoryEntryInMap(dir, parts[i])
if err != nil {
return nil, false
}
dir.SubDirectories[parts[i]] = sub dir.SubDirectories[parts[i]] = sub
created = true created = true
} }