From 5d197dd063c52b3bda27f773ca00a5f7814415a9 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 1 Feb 2015 03:10:28 -0800 Subject: [PATCH] Reply back errors properly --- pkg/api/minioapi/minioapi.go | 19 +++++++++++++++---- pkg/storage/fs/fs.go | 5 +++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pkg/api/minioapi/minioapi.go b/pkg/api/minioapi/minioapi.go index d3d2acf8f..8389e61be 100644 --- a/pkg/api/minioapi/minioapi.go +++ b/pkg/api/minioapi/minioapi.go @@ -105,13 +105,22 @@ func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Requ acceptsContentType := getContentType(req) objects, isTruncated, err := server.storage.ListObjects(bucket, prefix, 1000) - if err != nil { + switch err := err.(type) { + case nil: // success + response := generateObjectsListResult(bucket, objects, isTruncated) + w.Write(writeObjectHeadersAndResponse(w, response, acceptsContentType)) + case mstorage.BucketNotFound: + log.Println(err) + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(err.Error())) + case mstorage.ImplementationError: log.Println(err) w.WriteHeader(http.StatusInternalServerError) - return + w.Write([]byte(err.Error())) + default: + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) } - response := generateObjectsListResult(bucket, objects, isTruncated) - w.Write(writeObjectHeadersAndResponse(w, response, acceptsContentType)) } func (server *minioApi) putBucketHandler(w http.ResponseWriter, req *http.Request) { @@ -146,11 +155,13 @@ func (server *minioApi) getObjectHandler(w http.ResponseWriter, req *http.Reques { log.Println(err) w.WriteHeader(http.StatusNotFound) + w.Write([]byte(err.Error())) } default: { log.Println(err) w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) } } } diff --git a/pkg/storage/fs/fs.go b/pkg/storage/fs/fs.go index 4e1d450dd..475a38470 100644 --- a/pkg/storage/fs/fs.go +++ b/pkg/storage/fs/fs.go @@ -153,6 +153,11 @@ func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorag rootPrefix := path.Join(storage.root, bucket) + // check bucket exists + if _, err := os.Stat(rootPrefix); os.IsNotExist(err) { + return []mstorage.ObjectMetadata{}, false, mstorage.BucketNotFound{Bucket: bucket} + } + files, err := ioutil.ReadDir(rootPrefix) if err != nil { return []mstorage.ObjectMetadata{}, false, mstorage.EmbedError("bucket", "", err)