From 59ca65da3c59a27ace610106148ae2ff3f34ff07 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 25 Sep 2012 16:05:31 -0700 Subject: [PATCH] error handling --- weed-fs/src/cmd/weed/master.go | 21 +++++++++++---------- weed-fs/src/cmd/weed/volume.go | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/weed-fs/src/cmd/weed/master.go b/weed-fs/src/cmd/weed/master.go index 5b69b3d12..9bc7acc3e 100644 --- a/weed-fs/src/cmd/weed/master.go +++ b/weed-fs/src/cmd/weed/master.go @@ -45,17 +45,18 @@ func dirLookupHandler(w http.ResponseWriter, r *http.Request) { if commaSep > 0 { vid = vid[0:commaSep] } - volumeId, _ := storage.NewVolumeId(vid) - machines := topo.Lookup(volumeId) - if machines != nil { - ret := []map[string]string{} - for _, dn := range *machines { - ret = append(ret, map[string]string{"url": dn.Ip + strconv.Itoa(dn.Port), "publicUrl": dn.PublicUrl}) + volumeId, err := storage.NewVolumeId(vid) + if err == nil { + machines := topo.Lookup(volumeId) + if machines != nil { + ret := []map[string]string{} + for _, dn := range *machines { + ret = append(ret, map[string]string{"url": dn.Ip + strconv.Itoa(dn.Port), "publicUrl": dn.PublicUrl}) + } + writeJson(w, r, map[string]interface{}{"locations": ret}) + } else { + writeJson(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "}) } - writeJson(w, r, map[string]interface{}{"locations": ret}) - } else { - log.Println("Invalid volume id", volumeId) - writeJson(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "}) } } diff --git a/weed-fs/src/cmd/weed/volume.go b/weed-fs/src/cmd/weed/volume.go index a6d3e613a..ed3d9a081 100644 --- a/weed-fs/src/cmd/weed/volume.go +++ b/weed-fs/src/cmd/weed/volume.go @@ -65,21 +65,29 @@ func storeHandler(w http.ResponseWriter, r *http.Request) { func GetHandler(w http.ResponseWriter, r *http.Request) { n := new(storage.Needle) vid, fid, ext := parseURLPath(r.URL.Path) - volumeId, _ := storage.NewVolumeId(vid) + volumeId, err := storage.NewVolumeId(vid) + if err != nil { + if *IsDebug { + log.Println("parsing error:", err, r.URL.Path) + } + return + } n.ParsePath(fid) if *IsDebug { log.Println("volume", volumeId, "reading", n) } if !store.HasVolume(volumeId) { - lookupResult, err := operation.Lookup(*server, volumeId) + lookupResult, err := operation.Lookup(*masterNode, volumeId) if *IsDebug { log.Println("volume", volumeId, "found on", lookupResult, "error", err) } if err == nil { http.Redirect(w, r, "http://"+lookupResult.Locations[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently) } else { - log.Println("lookup error:", err) + if *IsDebug { + log.Println("lookup error:", err, r.URL.Path) + } w.WriteHeader(http.StatusNotFound) } return @@ -90,7 +98,7 @@ func GetHandler(w http.ResponseWriter, r *http.Request) { log.Println("read bytes", count, "error", e) } if e != nil || count <= 0 { - log.Println("read error:", e) + log.Println("read error:", e) w.WriteHeader(http.StatusNotFound) return }