Improve error handling
This commit is contained in:
parent
dadd54eb01
commit
47917a599c
3 changed files with 45 additions and 22 deletions
41
api.go
41
api.go
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
@ -25,13 +26,15 @@ func (s *Server) CreateBucketHandler() http.Handler {
|
|||
|
||||
err := json.NewDecoder(r.Body).Decode(&bucket)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
log.Println("error decoding json:", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = s.S3.MakeBucket(bucket.Name, "")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error making bucket:", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -40,7 +43,8 @@ func (s *Server) CreateBucketHandler() http.Handler {
|
|||
|
||||
err = json.NewEncoder(w).Encode(bucket)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error encoding json:", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
@ -56,7 +60,8 @@ func (s *Server) CreateObjectHandler() http.Handler {
|
|||
|
||||
err := json.NewDecoder(r.Body).Decode(©)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
log.Println("error decoding json:", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -66,7 +71,8 @@ func (s *Server) CreateObjectHandler() http.Handler {
|
|||
fmt.Println(objectSource)
|
||||
err = s.S3.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error copying object:", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -74,25 +80,30 @@ func (s *Server) CreateObjectHandler() http.Handler {
|
|||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(copy)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error encoding json:", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
err := r.ParseMultipartForm(32 << 20)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
log.Println("error parsing form:", err)
|
||||
return
|
||||
}
|
||||
|
||||
file, handler, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
log.Println("error getting form file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = s.S3.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error putting object:", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -108,7 +119,8 @@ func (s *Server) DeleteBucketHandler() http.Handler {
|
|||
|
||||
err := s.S3.RemoveBucket(vars["bucketName"])
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error removing bucket:", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -123,7 +135,8 @@ func (s *Server) DeleteObjectHandler() http.Handler {
|
|||
|
||||
err := s.S3.RemoveObject(vars["bucketName"], vars["objectName"])
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error removing object:", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -139,7 +152,8 @@ func (s *Server) GetObjectHandler() http.Handler {
|
|||
|
||||
object, err := s.S3.GetObject(vars["bucketName"], objectName)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error getting object:", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -148,7 +162,8 @@ func (s *Server) GetObjectHandler() http.Handler {
|
|||
|
||||
_, err = io.Copy(w, object)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error copying object:", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
|
7
minio.go
7
minio.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
minio "github.com/minio/minio-go"
|
||||
|
@ -18,12 +19,12 @@ func NewMinioClient() *minio.Client {
|
|||
|
||||
s3AccessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
|
||||
if len(s3AccessKeyID) == 0 {
|
||||
panic("Please set S3_ACCESS_KEY_ID")
|
||||
log.Fatal("Please set S3_ACCESS_KEY_ID")
|
||||
}
|
||||
|
||||
s3SecretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
|
||||
if len(s3SecretAccessKey) == 0 {
|
||||
panic("Please set S3_SECRET_ACCESS_KEY")
|
||||
log.Fatal("Please set S3_SECRET_ACCESS_KEY")
|
||||
}
|
||||
|
||||
if os.Getenv("V2_SIGNING") == "true" {
|
||||
|
@ -32,7 +33,7 @@ func NewMinioClient() *minio.Client {
|
|||
client, err = minio.New(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return client
|
||||
|
|
19
pages.go
19
pages.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
|
@ -32,7 +33,8 @@ func (s *Server) BucketPageHandler() http.Handler {
|
|||
|
||||
t, err := template.ParseFiles(lp, bp)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error parsing templates:", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -41,7 +43,8 @@ func (s *Server) BucketPageHandler() http.Handler {
|
|||
objectCh := s.S3.ListObjectsV2(bucketName, "", false, doneCh)
|
||||
for object := range objectCh {
|
||||
if object.Err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error listing objects:", err)
|
||||
return
|
||||
}
|
||||
objectWithIcon := ObjectWithIcon{object, icon(object.Key)}
|
||||
|
@ -55,7 +58,8 @@ func (s *Server) BucketPageHandler() http.Handler {
|
|||
|
||||
err = t.ExecuteTemplate(w, "layout", bucketPage)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error executing template:", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
@ -69,19 +73,22 @@ func (s *Server) BucketsPageHandler() http.Handler {
|
|||
|
||||
t, err := template.ParseFiles(lp, ip)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error parsing templates:", err)
|
||||
return
|
||||
}
|
||||
|
||||
buckets, err := s.S3.ListBuckets()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error listing buckets:", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = t.ExecuteTemplate(w, "layout", buckets)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("error executing template:", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue