Externalize error handling

This commit is contained in:
Lena Fuhrimann 2017-03-30 14:00:06 +02:00
parent 8360fc0ad3
commit f4198ac46d
4 changed files with 60 additions and 49 deletions

56
api.go
View file

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -26,15 +25,15 @@ func (s *Server) CreateBucketHandler() http.Handler {
err := json.NewDecoder(r.Body).Decode(&bucket) err := json.NewDecoder(r.Body).Decode(&bucket)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity) msg := "error decoding json"
log.Println("error decoding json:", err) handleHTTPError(w, msg, err, http.StatusUnprocessableEntity)
return return
} }
err = s.S3.MakeBucket(bucket.Name, "") err = s.S3.MakeBucket(bucket.Name, "")
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error making bucket"
log.Println("error making bucket:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
@ -43,8 +42,8 @@ func (s *Server) CreateBucketHandler() http.Handler {
err = json.NewEncoder(w).Encode(bucket) err = json.NewEncoder(w).Encode(bucket)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error encoding json"
log.Println("error encoding json:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
}) })
@ -60,50 +59,49 @@ func (s *Server) CreateObjectHandler() http.Handler {
err := json.NewDecoder(r.Body).Decode(&copy) err := json.NewDecoder(r.Body).Decode(&copy)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity) msg := "error decoding json"
log.Println("error decoding json:", err) handleHTTPError(w, msg, err, http.StatusUnprocessableEntity)
return return
} }
var copyConds = minio.NewCopyConditions() var copyConds = minio.NewCopyConditions()
objectSource := fmt.Sprintf("/%s/%s", copy.SourceBucketName, copy.SourceObjectName) 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) err = s.S3.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error copying object"
log.Println("error copying object:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(copy) err = json.NewEncoder(w).Encode(copy)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error encoding json"
log.Println("error encoding json:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
} else { } else {
err := r.ParseMultipartForm(32 << 20) err := r.ParseMultipartForm(32 << 20)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity) msg := "error parsing form"
log.Println("error parsing form:", err) handleHTTPError(w, msg, err, http.StatusUnprocessableEntity)
return return
} }
file, handler, err := r.FormFile("file") file, handler, err := r.FormFile("file")
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity) msg := "error getting form file"
log.Println("error getting form file:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
defer file.Close() defer file.Close()
_, err = s.S3.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream") _, err = s.S3.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream")
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error putting object"
log.Println("error putting object:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
@ -119,8 +117,8 @@ func (s *Server) DeleteBucketHandler() http.Handler {
err := s.S3.RemoveBucket(vars["bucketName"]) err := s.S3.RemoveBucket(vars["bucketName"])
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error removing bucket"
log.Println("error removing bucket:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
@ -135,8 +133,8 @@ func (s *Server) DeleteObjectHandler() http.Handler {
err := s.S3.RemoveObject(vars["bucketName"], vars["objectName"]) err := s.S3.RemoveObject(vars["bucketName"], vars["objectName"])
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error removing object"
log.Println("error removing object:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
@ -152,8 +150,8 @@ func (s *Server) GetObjectHandler() http.Handler {
object, err := s.S3.GetObject(vars["bucketName"], objectName) object, err := s.S3.GetObject(vars["bucketName"], objectName)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error getting object"
log.Println("error getting object:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
@ -162,8 +160,8 @@ func (s *Server) GetObjectHandler() http.Handler {
_, err = io.Copy(w, object) _, err = io.Copy(w, object)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error copying object"
log.Println("error copying object:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
}) })

16
errors.go Normal file
View file

@ -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)
}
}

11
main.go
View file

@ -15,17 +15,12 @@ type Server struct {
} }
func main() { func main() {
port := os.Getenv("PORT")
if len(port) == 0 {
port = "8080"
}
s := &Server{ s := &Server{
S3: NewMinioClient(), S3: NewMinioClient(),
} }
router := mux.NewRouter()
logger := log.New(os.Stdout, "request: ", log.Lshortfile) logger := log.New(os.Stdout, "request: ", log.Lshortfile)
router := mux.NewRouter()
router. router.
Methods("GET"). Methods("GET").
@ -64,5 +59,9 @@ func main() {
Path("/{bucketName}/objects/{objectName}"). Path("/{bucketName}/objects/{objectName}").
Handler(Adapt(s.DeleteObjectHandler(), Logging(logger))) Handler(Adapt(s.DeleteObjectHandler(), Logging(logger)))
port := os.Getenv("PORT")
if len(port) == 0 {
port = "8080"
}
log.Fatal(http.ListenAndServe(":"+port, router)) log.Fatal(http.ListenAndServe(":"+port, router))
} }

View file

@ -2,7 +2,6 @@ package main
import ( import (
"html/template" "html/template"
"log"
"net/http" "net/http"
"path" "path"
@ -33,18 +32,17 @@ func (s *Server) BucketPageHandler() http.Handler {
t, err := template.ParseFiles(lp, bp) t, err := template.ParseFiles(lp, bp)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error parsing templates"
log.Println("error parsing templates:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
doneCh := make(chan struct{}) doneCh := make(chan struct{})
objectCh := s.S3.ListObjectsV2(bucketName, "", false, doneCh) objectCh := s.S3.ListObjectsV2(bucketName, "", false, doneCh)
for object := range objectCh { for object := range objectCh {
if object.Err != nil { if object.Err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error listing objects"
log.Println("error listing objects:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
objectWithIcon := ObjectWithIcon{object, icon(object.Key)} objectWithIcon := ObjectWithIcon{object, icon(object.Key)}
@ -58,8 +56,8 @@ func (s *Server) BucketPageHandler() http.Handler {
err = t.ExecuteTemplate(w, "layout", bucketPage) err = t.ExecuteTemplate(w, "layout", bucketPage)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error executing template"
log.Println("error executing template:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
}) })
@ -73,22 +71,22 @@ func (s *Server) BucketsPageHandler() http.Handler {
t, err := template.ParseFiles(lp, ip) t, err := template.ParseFiles(lp, ip)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error parsing templates"
log.Println("error parsing templates:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
buckets, err := s.S3.ListBuckets() buckets, err := s.S3.ListBuckets()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error listing buckets"
log.Println("error listing buckets:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
err = t.ExecuteTemplate(w, "layout", buckets) err = t.ExecuteTemplate(w, "layout", buckets)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) msg := "error executing template"
log.Println("error executing template:", err) handleHTTPError(w, msg, err, http.StatusInternalServerError)
return return
} }
}) })