Adding error handling to storage get call

master
Frederick F. Kautz IV 10 years ago
parent cc288a5fd7
commit 36268f07ea
  1. 19
      pkg/storage/storage.go
  2. 20
      pkg/webapi/minioapi/minioapi.go

@ -10,16 +10,27 @@ type Storage struct {
data map[string][]byte
}
func (storage *Storage) CopyObjectToWriter(w io.Writer, bucket string, object string) error {
type GenericError struct {
bucket string
path string
}
type ObjectNotFound GenericError
func (self ObjectNotFound) Error() string {
return "Not Found: " + self.bucket + "#" + self.path
}
func (storage *Storage) CopyObjectToWriter(w io.Writer, bucket string, object string) (int64, error) {
// TODO synchronize access
// get object
key := bucket + ":" + object
if val, ok := storage.data[key]; ok {
objectBuffer := bytes.NewBuffer(val)
_, err := io.Copy(w, objectBuffer)
return err
written, err := io.Copy(w, objectBuffer)
return written, err
} else {
return errors.New("Not Found")
return 0, ObjectNotFound{bucket: bucket, path: object}
}
}

@ -1,6 +1,7 @@
package minioapi
import (
"log"
"net/http"
"github.com/gorilla/mux"
@ -25,7 +26,24 @@ func (server *minioApi) getObjectHandler(w http.ResponseWriter, req *http.Reques
vars := mux.Vars(req)
bucket := vars["bucket"]
object := vars["object"]
server.storage.CopyObjectToWriter(w, bucket, object)
_, err := server.storage.CopyObjectToWriter(w, bucket, object)
switch err := err.(type) {
case nil: // success
{
log.Println("Found: " + bucket + "#" + object)
}
case mstorage.ObjectNotFound:
{
log.Println(err)
w.WriteHeader(http.StatusNotFound)
}
default:
{
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
}
}
func (server *minioApi) putObjectHandler(w http.ResponseWriter, req *http.Request) {

Loading…
Cancel
Save