Adding better error support to api

This commit is contained in:
Frederick F. Kautz IV 2015-04-03 18:06:30 -07:00
parent a29990e4bc
commit 5865295f5d
5 changed files with 79 additions and 14 deletions

View File

@ -20,6 +20,7 @@ import (
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/minio-io/iodine"
"github.com/minio-io/minio/pkg/drivers" "github.com/minio-io/minio/pkg/drivers"
"github.com/minio-io/minio/pkg/utils/log" "github.com/minio-io/minio/pkg/utils/log"
) )
@ -69,10 +70,6 @@ func (server *minioAPI) listObjectsHandler(w http.ResponseWriter, req *http.Requ
{ {
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path)
} }
case drivers.ImplementationError:
{
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
}
case drivers.BucketNameInvalid: case drivers.BucketNameInvalid:
{ {
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path)
@ -83,7 +80,7 @@ func (server *minioAPI) listObjectsHandler(w http.ResponseWriter, req *http.Requ
} }
default: default:
{ {
log.Error.Println(err) log.Error.Println(iodine.New(err, nil))
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
} }
} }

View File

@ -21,6 +21,7 @@ import (
"net/http" "net/http"
router "github.com/gorilla/mux" router "github.com/gorilla/mux"
"github.com/minio-io/iodine"
"github.com/minio-io/minio/pkg/api/config" "github.com/minio-io/minio/pkg/api/config"
"github.com/minio-io/minio/pkg/drivers" "github.com/minio-io/minio/pkg/drivers"
) )
@ -83,7 +84,7 @@ func HTTPHandler(domain string, driver drivers.Driver) http.Handler {
var conf = config.Config{} var conf = config.Config{}
if err := conf.SetupConfig(); err != nil { if err := conf.SetupConfig(); err != nil {
log.Fatal(err) log.Fatal(iodine.New(err, map[string]string{"domain": domain}))
} }
return validateHandler(conf, ignoreResourcesHandler(mux)) return validateHandler(conf, ignoreResourcesHandler(mux))

View File

@ -893,6 +893,66 @@ func (s *MySuite) TestPartialContent(c *C) {
c.Assert(string(partialObject), Equals, "wo") c.Assert(string(partialObject), Equals, "wo")
} }
func (s *MySuite) TestListObjectsHandlerErrors(c *C) {
switch driver := s.Driver.(type) {
case *mocks.Driver:
{
driver.AssertExpectations(c)
}
default:
{
// We mock failures here to test
return
}
}
driver := s.Driver
typedDriver := s.MockDriver
httpHandler := api.HTTPHandler("", driver)
testServer := httptest.NewServer(httpHandler)
defer testServer.Close()
client := http.Client{}
typedDriver.On("ListObjects", "foo", mock.Anything).Return(make([]drivers.ObjectMetadata, 0), drivers.BucketResourcesMetadata{}, drivers.BucketNotFound{}).Once()
request, err := http.NewRequest("GET", testServer.URL+"/foo", bytes.NewBufferString(""))
c.Assert(err, IsNil)
response, err := client.Do(request)
verifyError(c, response, "NoSuchBucket", "The specified bucket does not exist.", http.StatusNotFound)
typedDriver.On("ListObjects", "foo", mock.Anything).Return(make([]drivers.ObjectMetadata, 0), drivers.BucketResourcesMetadata{}, drivers.BucketNameInvalid{}).Once()
request, err = http.NewRequest("GET", testServer.URL+"/foo", bytes.NewBufferString(""))
c.Assert(err, IsNil)
response, err = client.Do(request)
c.Assert(err, IsNil)
verifyError(c, response, "InvalidBucketName", "The specified bucket is not valid.", http.StatusBadRequest)
typedDriver.On("ListObjects", "foo", mock.Anything).Return(make([]drivers.ObjectMetadata, 0), drivers.BucketResourcesMetadata{}, drivers.ObjectNameInvalid{}).Once()
request, err = http.NewRequest("GET", testServer.URL+"/foo", bytes.NewBufferString(""))
c.Assert(err, IsNil)
response, err = client.Do(request)
c.Assert(err, IsNil)
verifyError(c, response, "NoSuchKey", "The specified key does not exist.", http.StatusNotFound)
typedDriver.On("ListObjects", "foo", mock.Anything).Return(make([]drivers.ObjectMetadata, 0), drivers.BucketResourcesMetadata{}, drivers.BackendCorrupted{}).Once()
request, err = http.NewRequest("GET", testServer.URL+"/foo", bytes.NewBufferString(""))
c.Assert(err, IsNil)
response, err = client.Do(request)
c.Assert(err, IsNil)
verifyError(c, response, "InternalError", "We encountered an internal error, please try again.", http.StatusInternalServerError)
}
func verifyError(c *C, response *http.Response, code, description string, statusCode int) {
data, err := ioutil.ReadAll(response.Body)
c.Assert(err, IsNil)
errorResponse := api.ErrorResponse{}
err = xml.Unmarshal(data, &errorResponse)
c.Assert(err, IsNil)
c.Assert(errorResponse.Code, Equals, code)
c.Assert(errorResponse.Message, Equals, description)
c.Assert(response.StatusCode, Equals, statusCode)
}
func startMockDriver() *mocks.Driver { func startMockDriver() *mocks.Driver {
return &mocks.Driver{ return &mocks.Driver{
ObjectWriterData: make(map[string][]byte), ObjectWriterData: make(map[string][]byte),

View File

@ -18,6 +18,7 @@ package config
import ( import (
"encoding/json" "encoding/json"
"github.com/minio-io/iodine"
"io" "io"
"os" "os"
"os/user" "os/user"
@ -44,12 +45,12 @@ type User struct {
func (c *Config) SetupConfig() error { func (c *Config) SetupConfig() error {
u, err := user.Current() u, err := user.Current()
if err != nil { if err != nil {
return err return iodine.New(err, nil)
} }
confPath := path.Join(u.HomeDir, ".minio") confPath := path.Join(u.HomeDir, ".minio")
if err := os.MkdirAll(confPath, os.ModeDir); err != nil { if err := os.MkdirAll(confPath, os.ModeDir); err != nil {
return err return iodine.New(err, nil)
} }
c.ConfigPath = confPath c.ConfigPath = confPath
@ -57,7 +58,7 @@ func (c *Config) SetupConfig() error {
if _, err := os.Stat(c.ConfigFile); os.IsNotExist(err) { if _, err := os.Stat(c.ConfigFile); os.IsNotExist(err) {
_, err = os.Create(c.ConfigFile) _, err = os.Create(c.ConfigFile)
if err != nil { if err != nil {
return err return iodine.New(err, nil)
} }
} }
@ -113,7 +114,7 @@ func (c *Config) WriteConfig() error {
file, err = os.OpenFile(c.ConfigFile, os.O_WRONLY, 0666) file, err = os.OpenFile(c.ConfigFile, os.O_WRONLY, 0666)
defer file.Close() defer file.Close()
if err != nil { if err != nil {
return err return iodine.New(err, nil)
} }
encoder := json.NewEncoder(file) encoder := json.NewEncoder(file)
@ -132,7 +133,7 @@ func (c *Config) ReadConfig() error {
file, err = os.OpenFile(c.ConfigFile, os.O_RDONLY, 0666) file, err = os.OpenFile(c.ConfigFile, os.O_RDONLY, 0666)
defer file.Close() defer file.Close()
if err != nil { if err != nil {
return err return iodine.New(err, nil)
} }
users := make(map[string]User) users := make(map[string]User)
@ -145,6 +146,6 @@ func (c *Config) ReadConfig() error {
c.Users = users c.Users = users
return nil return nil
default: default:
return err return iodine.New(err, nil)
} }
} }

View File

@ -19,13 +19,14 @@ package web
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"log"
"net/http" "net/http"
"path" "path"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/minio-io/iodine"
"github.com/minio-io/minio/pkg/api/config" "github.com/minio-io/minio/pkg/api/config"
"github.com/minio-io/minio/pkg/utils/crypto/keys" "github.com/minio-io/minio/pkg/utils/crypto/keys"
"github.com/minio-io/minio/pkg/utils/log"
) )
const ( const (
@ -48,7 +49,7 @@ func HTTPHandler() http.Handler {
var api = webAPI{} var api = webAPI{}
if err := api.conf.SetupConfig(); err != nil { if err := api.conf.SetupConfig(); err != nil {
log.Fatal(err) log.Fatal(iodine.New(err, nil))
} }
api.webPath = path.Join(api.conf.GetConfigPath(), defaultWeb) api.webPath = path.Join(api.conf.GetConfigPath(), defaultWeb)
@ -79,6 +80,7 @@ func (web *webAPI) accessHandler(w http.ResponseWriter, req *http.Request) {
err = web.conf.ReadConfig() err = web.conf.ReadConfig()
if err != nil { if err != nil {
log.Error.Println(iodine.New(err, nil))
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
@ -94,6 +96,7 @@ func (web *webAPI) accessHandler(w http.ResponseWriter, req *http.Request) {
accesskey, err = keys.GenerateRandomAlphaNumeric(keys.MinioAccessID) accesskey, err = keys.GenerateRandomAlphaNumeric(keys.MinioAccessID)
if err != nil { if err != nil {
log.Error.Println(iodine.New(err, nil))
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
@ -102,6 +105,7 @@ func (web *webAPI) accessHandler(w http.ResponseWriter, req *http.Request) {
secretkey, err = keys.GenerateRandomBase64(keys.MinioSecretID) secretkey, err = keys.GenerateRandomBase64(keys.MinioSecretID)
if err != nil { if err != nil {
log.Error.Println(iodine.New(err, nil))
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
@ -111,6 +115,7 @@ func (web *webAPI) accessHandler(w http.ResponseWriter, req *http.Request) {
web.conf.AddUser(user) web.conf.AddUser(user)
err = web.conf.WriteConfig() err = web.conf.WriteConfig()
if err != nil { if err != nil {
log.Error.Println(iodine.New(err, nil))
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
@ -118,6 +123,7 @@ func (web *webAPI) accessHandler(w http.ResponseWriter, req *http.Request) {
err = web.conf.ReadConfig() err = web.conf.ReadConfig()
if err != nil { if err != nil {
log.Error.Println(iodine.New(err, nil))
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return