From f4198ac46d5982e8d36d10df142c7e2f5644bc29 Mon Sep 17 00:00:00 2001 From: Lena Fuhrimann <6780471+cloudlena@users.noreply.github.com> Date: Thu, 30 Mar 2017 14:00:06 +0200 Subject: [PATCH] Externalize error handling --- api.go | 56 +++++++++++++++++++++++++++---------------------------- errors.go | 16 ++++++++++++++++ main.go | 11 +++++------ pages.go | 26 ++++++++++++-------------- 4 files changed, 60 insertions(+), 49 deletions(-) create mode 100644 errors.go diff --git a/api.go b/api.go index fad93be..d71db29 100644 --- a/api.go +++ b/api.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "log" "net/http" "github.com/gorilla/mux" @@ -26,15 +25,15 @@ func (s *Server) CreateBucketHandler() http.Handler { err := json.NewDecoder(r.Body).Decode(&bucket) if err != nil { - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - log.Println("error decoding json:", err) + msg := "error decoding json" + handleHTTPError(w, msg, err, http.StatusUnprocessableEntity) return } err = s.S3.MakeBucket(bucket.Name, "") if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error making bucket:", err) + msg := "error making bucket" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } @@ -43,8 +42,8 @@ func (s *Server) CreateBucketHandler() http.Handler { err = json.NewEncoder(w).Encode(bucket) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error encoding json:", err) + msg := "error encoding json" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } }) @@ -60,50 +59,49 @@ func (s *Server) CreateObjectHandler() http.Handler { err := json.NewDecoder(r.Body).Decode(©) if err != nil { - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - log.Println("error decoding json:", err) + msg := "error decoding json" + handleHTTPError(w, msg, err, http.StatusUnprocessableEntity) return } var copyConds = minio.NewCopyConditions() objectSource := fmt.Sprintf("/%s/%s", copy.SourceBucketName, copy.SourceObjectName) - fmt.Println(copy) - fmt.Println(objectSource) err = s.S3.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error copying object:", err) + msg := "error copying object" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(copy) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error encoding json:", err) + msg := "error encoding json" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } } else { err := r.ParseMultipartForm(32 << 20) if err != nil { - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - log.Println("error parsing form:", err) + msg := "error parsing form" + handleHTTPError(w, msg, err, http.StatusUnprocessableEntity) return } file, handler, err := r.FormFile("file") if err != nil { - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - log.Println("error getting form file:", err) + msg := "error getting form file" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } defer file.Close() _, err = s.S3.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream") if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error putting object:", err) + msg := "error putting object" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } @@ -119,8 +117,8 @@ func (s *Server) DeleteBucketHandler() http.Handler { err := s.S3.RemoveBucket(vars["bucketName"]) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error removing bucket:", err) + msg := "error removing bucket" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } @@ -135,8 +133,8 @@ func (s *Server) DeleteObjectHandler() http.Handler { err := s.S3.RemoveObject(vars["bucketName"], vars["objectName"]) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error removing object:", err) + msg := "error removing object" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } @@ -152,8 +150,8 @@ func (s *Server) GetObjectHandler() http.Handler { object, err := s.S3.GetObject(vars["bucketName"], objectName) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error getting object:", err) + msg := "error getting object" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } @@ -162,8 +160,8 @@ func (s *Server) GetObjectHandler() http.Handler { _, err = io.Copy(w, object) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error copying object:", err) + msg := "error copying object" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } }) diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..aa12832 --- /dev/null +++ b/errors.go @@ -0,0 +1,16 @@ +package main + +import ( + "log" + "net/http" +) + +// handleHTTPError handles HTTP errors +func handleHTTPError(w http.ResponseWriter, msg string, err error, statusCode int) { + http.Error(w, msg, statusCode) + if err != nil { + log.Println(msg+":", err.Error()) + } else { + log.Println(msg) + } +} diff --git a/main.go b/main.go index 76f4158..7e50ed5 100644 --- a/main.go +++ b/main.go @@ -15,17 +15,12 @@ type Server struct { } func main() { - port := os.Getenv("PORT") - if len(port) == 0 { - port = "8080" - } - s := &Server{ S3: NewMinioClient(), } - router := mux.NewRouter() logger := log.New(os.Stdout, "request: ", log.Lshortfile) + router := mux.NewRouter() router. Methods("GET"). @@ -64,5 +59,9 @@ func main() { Path("/{bucketName}/objects/{objectName}"). Handler(Adapt(s.DeleteObjectHandler(), Logging(logger))) + port := os.Getenv("PORT") + if len(port) == 0 { + port = "8080" + } log.Fatal(http.ListenAndServe(":"+port, router)) } diff --git a/pages.go b/pages.go index 7f37f00..be40566 100644 --- a/pages.go +++ b/pages.go @@ -2,7 +2,6 @@ package main import ( "html/template" - "log" "net/http" "path" @@ -33,18 +32,17 @@ func (s *Server) BucketPageHandler() http.Handler { t, err := template.ParseFiles(lp, bp) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error parsing templates:", err) + msg := "error parsing templates" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } doneCh := make(chan struct{}) - objectCh := s.S3.ListObjectsV2(bucketName, "", false, doneCh) for object := range objectCh { if object.Err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error listing objects:", err) + msg := "error listing objects" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } objectWithIcon := ObjectWithIcon{object, icon(object.Key)} @@ -58,8 +56,8 @@ func (s *Server) BucketPageHandler() http.Handler { err = t.ExecuteTemplate(w, "layout", bucketPage) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error executing template:", err) + msg := "error executing template" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } }) @@ -73,22 +71,22 @@ func (s *Server) BucketsPageHandler() http.Handler { t, err := template.ParseFiles(lp, ip) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error parsing templates:", err) + msg := "error parsing templates" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } buckets, err := s.S3.ListBuckets() if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error listing buckets:", err) + msg := "error listing buckets" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } err = t.ExecuteTemplate(w, "layout", buckets) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - log.Println("error executing template:", err) + msg := "error executing template" + handleHTTPError(w, msg, err, http.StatusInternalServerError) return } })