From 20706d8cf267d893390ffe41f93ab684aca6eb25 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 26 Sep 2012 14:28:46 -0700 Subject: [PATCH] more error handling --- weed-fs/src/cmd/weed/master.go | 1 + weed-fs/src/cmd/weed/upload.go | 24 ++++++++++++++------- weed-fs/src/cmd/weed/volume.go | 6 +++++- weed-fs/src/pkg/operation/upload_content.go | 5 +++++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/weed-fs/src/cmd/weed/master.go b/weed-fs/src/cmd/weed/master.go index d866e1580..5fdc80c20 100644 --- a/weed-fs/src/cmd/weed/master.go +++ b/weed-fs/src/cmd/weed/master.go @@ -77,6 +77,7 @@ func dirAssignHandler(w http.ResponseWriter, r *http.Request) { if topo.GetVolumeLayout(rt).GetActiveVolumeCount() <= 0 { if topo.FreeSpace() <= 0 { writeJson(w, r, map[string]string{"error": "No free volumes left!"}) + return } else { vg.GrowByType(rt, topo) } diff --git a/weed-fs/src/cmd/weed/upload.go b/weed-fs/src/cmd/weed/upload.go index 89e0c34fc..78b2452ff 100644 --- a/weed-fs/src/cmd/weed/upload.go +++ b/weed-fs/src/cmd/weed/upload.go @@ -6,7 +6,7 @@ import ( "fmt" "net/url" "os" - "pkg/operation" + "pkg/operation" "pkg/util" "strconv" ) @@ -60,7 +60,7 @@ func assign(count int) (*AssignResult, error) { return &ret, nil } -func upload(filename string, server string, fid string) (int) { +func upload(filename string, server string, fid string) (int, error) { if *IsDebug { fmt.Println("Start uploading file:", filename) } @@ -69,15 +69,19 @@ func upload(filename string, server string, fid string) (int) { if *IsDebug { fmt.Println("Failed to open file:", filename) } - panic(err.Error()) + return 0, err } - ret, _ := operation.Upload("http://"+server+"/"+fid, filename, fh) - return ret.Size + ret, e := operation.Upload("http://"+server+"/"+fid, filename, fh) + if e != nil { + return 0, e + } + return ret.Size, e } type SubmitResult struct { - Fid string "fid" - Size int "size" + Fid string "fid" + Size int "size" + Error string "error" } func submit(files []string) []SubmitResult { @@ -92,7 +96,11 @@ func submit(files []string) []SubmitResult { if index > 0 { fid = fid + "_" + strconv.Itoa(index) } - results[index].Size = upload(file, ret.PublicUrl, fid) + results[index].Size, err = upload(file, ret.PublicUrl, fid) + if err != nil { + fid = "" + results[index].Error = err.Error() + } results[index].Fid = fid } return results diff --git a/weed-fs/src/cmd/weed/volume.go b/weed-fs/src/cmd/weed/volume.go index ebe67b0b9..5e35d6631 100644 --- a/weed-fs/src/cmd/weed/volume.go +++ b/weed-fs/src/cmd/weed/volume.go @@ -133,6 +133,7 @@ func PostHandler(w http.ResponseWriter, r *http.Request) { writeJson(w, r, ne) } else { ret := store.Write(volumeId, needle) + errorStatus := "" if ret > 0 || !store.HasVolume(volumeId) { //send to other replica locations if r.FormValue("type") != "standard" { if distributedOperation(volumeId, func(location operation.Location) bool { @@ -142,16 +143,19 @@ func PostHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) } else { ret = 0 + errorStatus = "Failed to write to replicas for volume " + volumeId.String() w.WriteHeader(http.StatusInternalServerError) } } else { w.WriteHeader(http.StatusCreated) } } else { + errorStatus = "Failed to write to local disk" w.WriteHeader(http.StatusInternalServerError) } - m := make(map[string]uint32) + m := make(map[string]interface{}) m["size"] = ret + m["error"] = errorStatus writeJson(w, r, m) } } diff --git a/weed-fs/src/pkg/operation/upload_content.go b/weed-fs/src/pkg/operation/upload_content.go index 652cbe71b..7ed74e02f 100644 --- a/weed-fs/src/pkg/operation/upload_content.go +++ b/weed-fs/src/pkg/operation/upload_content.go @@ -9,10 +9,12 @@ import ( "log" "mime/multipart" "net/http" + "errors" ) type UploadResult struct { Size int + Error string } func Upload(uploadUrl string, filename string, reader io.Reader) (*UploadResult, error) { @@ -38,5 +40,8 @@ func Upload(uploadUrl string, filename string, reader io.Reader) (*UploadResult, log.Println("failing to read upload resonse", uploadUrl, resp_body) return nil, err } + if ret.Error != ""{ + return nil, errors.New(ret.Error) + } return &ret, nil }