Externalize error handling
This commit is contained in:
parent
8360fc0ad3
commit
f4198ac46d
4 changed files with 60 additions and 49 deletions
56
api.go
56
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
|
||||
}
|
||||
})
|
||||
|
|
16
errors.go
Normal file
16
errors.go
Normal 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
11
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))
|
||||
}
|
||||
|
|
26
pages.go
26
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
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue