From 9954e5c280adfa7dc0b4b3dd7008c4d93ff9d6e5 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Fri, 3 Apr 2015 18:53:21 -0700 Subject: [PATCH] Better error handling + tests for get object --- pkg/api/api_object_handlers.go | 10 +--- pkg/api/api_test.go | 102 +++++++++++++++++++++++++++++---- 2 files changed, 94 insertions(+), 18 deletions(-) diff --git a/pkg/api/api_object_handlers.go b/pkg/api/api_object_handlers.go index 06a08e10d..6b1952c50 100644 --- a/pkg/api/api_object_handlers.go +++ b/pkg/api/api_object_handlers.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) } } diff --git a/pkg/api/api_test.go b/pkg/api/api_test.go index fdb837c1c..367226012 100644 --- a/pkg/api/api_test.go +++ b/pkg/api/api_test.go @@ -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)