Better error handling + tests for get object

master
Frederick F. Kautz IV 10 years ago
parent 407251d51f
commit 9954e5c280
  1. 10
      pkg/api/api_object_handlers.go
  2. 102
      pkg/api/api_test.go

@ -61,9 +61,8 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
w.WriteHeader(http.StatusPartialContent)
_, err := server.driver.GetPartialObject(w, bucket, object, httpRange.start, httpRange.length)
if err != nil {
err = iodine.New(err, nil)
// unable to write headers, we've already printed data. Just close the connection.
log.Error.Println(err)
log.Error.Println(iodine.New(err, nil))
}
}
}
@ -84,14 +83,9 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
{
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path)
}
case drivers.ImplementationError:
{
log.Error.Println(err)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
}
default:
{
log.Error.Println(err)
log.Error.Println(iodine.New(err, nil))
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
}
}

@ -894,9 +894,18 @@ func (s *MySuite) TestPartialContent(c *C) {
}
func (s *MySuite) TestListObjectsHandlerErrors(c *C) {
driver := startMockDriver()
typedDriver := driver
defer driver.AssertExpectations(c)
switch driver := s.Driver.(type) {
case *mocks.Driver:
{
driver.AssertExpectations(c)
}
default:
{
return
}
}
driver := s.Driver
typedDriver := s.MockDriver
httpHandler := api.HTTPHandler("", driver)
testServer := httptest.NewServer(httpHandler)
@ -933,9 +942,18 @@ func (s *MySuite) TestListObjectsHandlerErrors(c *C) {
}
func (s *MySuite) TestListBucketsErrors(c *C) {
driver := startMockDriver()
typedDriver := driver
defer driver.AssertExpectations(c)
switch driver := s.Driver.(type) {
case *mocks.Driver:
{
driver.AssertExpectations(c)
}
default:
{
return
}
}
driver := s.Driver
typedDriver := s.MockDriver
httpHandler := api.HTTPHandler("", driver)
testServer := httptest.NewServer(httpHandler)
@ -950,10 +968,19 @@ func (s *MySuite) TestListBucketsErrors(c *C) {
verifyError(c, response, "InternalError", "We encountered an internal error, please try again.", http.StatusInternalServerError)
}
func (s *MySuite) TestPutBucketHandler(c *C) {
driver := startMockDriver()
typedDriver := driver
defer driver.AssertExpectations(c)
func (s *MySuite) TestPutBucketErrors(c *C) {
switch driver := s.Driver.(type) {
case *mocks.Driver:
{
driver.AssertExpectations(c)
}
default:
{
return
}
}
driver := s.Driver
typedDriver := s.MockDriver
httpHandler := api.HTTPHandler("", driver)
testServer := httptest.NewServer(httpHandler)
@ -982,6 +1009,61 @@ func (s *MySuite) TestPutBucketHandler(c *C) {
verifyError(c, response, "InternalError", "We encountered an internal error, please try again.", http.StatusInternalServerError)
}
func (s *MySuite) TestGetObjectErrors(c *C) {
switch driver := s.Driver.(type) {
case *mocks.Driver:
{
driver.AssertExpectations(c)
}
default:
{
return
}
}
driver := s.Driver
typedDriver := s.MockDriver
httpHandler := api.HTTPHandler("", driver)
testServer := httptest.NewServer(httpHandler)
defer testServer.Close()
client := http.Client{}
typedDriver.On("GetObjectMetadata", "foo", "bar", "").Return(drivers.ObjectMetadata{}, drivers.ObjectNotFound{}).Once()
request, err := http.NewRequest("GET", testServer.URL+"/foo/bar", 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("GetObjectMetadata", "foo", "bar", "").Return(drivers.ObjectMetadata{}, drivers.BucketNotFound{}).Once()
request, err = http.NewRequest("GET", testServer.URL+"/foo/bar", bytes.NewBufferString(""))
c.Assert(err, IsNil)
response, err = client.Do(request)
c.Assert(err, IsNil)
verifyError(c, response, "NoSuchBucket", "The specified bucket does not exist.", http.StatusNotFound)
typedDriver.On("GetObjectMetadata", "foo", "bar", "").Return(drivers.ObjectMetadata{}, drivers.ObjectNameInvalid{}).Once()
request, err = http.NewRequest("GET", testServer.URL+"/foo/bar", 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("GetObjectMetadata", "foo", "bar", "").Return(drivers.ObjectMetadata{}, drivers.BucketNameInvalid{}).Once()
request, err = http.NewRequest("GET", testServer.URL+"/foo/bar", 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("GetObjectMetadata", "foo", "bar", "").Return(drivers.ObjectMetadata{}, drivers.BackendCorrupted{}).Once()
request, err = http.NewRequest("GET", testServer.URL+"/foo/bar", 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)

Loading…
Cancel
Save