Consistent result format when error happens

This commit is contained in:
Chris Lu 2013-07-13 14:35:29 -07:00
parent 208ed2a991
commit 72490bc00f

View file

@ -92,11 +92,17 @@ type SubmitResult struct {
} }
func submit(files []string) ([]SubmitResult, error) { func submit(files []string) ([]SubmitResult, error) {
results := make([]SubmitResult, len(files))
for index, file := range files {
results[index].FileName = file
}
ret, err := assign(len(files)) ret, err := assign(len(files))
if err != nil { if err != nil {
return nil, err for index, _ := range files {
results[index].Error = err.Error()
}
return results, err
} }
results := make([]SubmitResult, len(files))
for index, file := range files { for index, file := range files {
fid := ret.Fid fid := ret.Fid
if index > 0 { if index > 0 {
@ -107,7 +113,6 @@ func submit(files []string) ([]SubmitResult, error) {
fid = "" fid = ""
results[index].Error = err.Error() results[index].Error = err.Error()
} }
results[index].FileName = file
results[index].Fid = fid results[index].Fid = fid
results[index].FileUrl = ret.PublicUrl + "/" + fid results[index].FileUrl = ret.PublicUrl + "/" + fid
} }
@ -121,25 +126,19 @@ func runUpload(cmd *Command, args []string) bool {
} }
filepath.Walk(*uploadDir, func(path string, info os.FileInfo, err error) error { filepath.Walk(*uploadDir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() { if !info.IsDir() {
if results, err := submit([]string{path}); err == nil { results, e := submit([]string{path})
bytes, _ := json.Marshal(results) bytes, _ := json.Marshal(results)
if bytes != nil { fmt.Println(string(bytes))
fmt.Println(string(bytes)) if e != nil {
} return e
} else {
fmt.Println(err, "when uploading", path)
return err
} }
} }
return err return err
}) })
} else { } else {
if results, err := submit(args); err == nil { results, _ := submit(args)
bytes, _ := json.Marshal(results) bytes, _ := json.Marshal(results)
fmt.Println(string(bytes)) fmt.Println(string(bytes))
} else {
fmt.Println(err)
}
} }
return true return true
} }