From dc48521d20ad04f69d4aefc4c317abbdc2fcb6bd Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 30 May 2015 19:56:11 -0700 Subject: [PATCH] Update to new changes in minio-go --- Godeps/Godeps.json | 2 +- .../src/github.com/minio/minio-go/api_test.go | 43 +++++++++++ .../{low_level_errors.go => errors.go} | 71 ++++++++++++------- .../minio/minio-go/low_level_api.go | 16 ++--- .../minio/minio-go/low_level_multipart_api.go | 10 +-- 5 files changed, 101 insertions(+), 41 deletions(-) rename Godeps/_workspace/src/github.com/minio/minio-go/{low_level_errors.go => errors.go} (59%) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 2afb53d5..ce087097 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -32,7 +32,7 @@ }, { "ImportPath": "github.com/minio/minio-go", - "Rev": "513ea3dc246f1aa25bfd0c21fe87011119b265ca" + "Rev": "6d3ba37e943a498d80330ce0b96d21213ee3e615" }, { "ImportPath": "github.com/minio/minio/pkg/iodine", diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go b/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go index a875339c..cf727f36 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go @@ -29,6 +29,7 @@ func ExampleGetPartSize() { fmt.Println(GetPartSize(5000000000)) // Output: 5242880 } + func ExampleGetPartSize_second() { fmt.Println(GetPartSize(50000000000000000)) // Output: 5368709120 @@ -206,6 +207,13 @@ func TestObjectOperations(t *testing.T) { if err != nil { t.Fatalf("Error") } + err = a.RemoveObject("bucket", "object1") + if err == nil { + t.Fatalf("Error") + } + if err.Error() != "404 Not Found" { + t.Errorf("Error") + } } func TestPartSize(t *testing.T) { @@ -261,3 +269,38 @@ func TestURLEncoding(t *testing.T) { } } } + +func TestErrorResponse(t *testing.T) { + errorResponse := []byte("AccessDeniedAccess Denied/mybucket/myphoto.jpgF19772218238A85AGuWkjyviSiGHizehqpmsD1ndz5NClSP19DOT+s2mv7gXGQ8/X1lhbDGiIJEXpGFD") + errorReader := bytes.NewReader(errorResponse) + err := responseToError(errorReader) + if err == nil { + t.Fatal("Error") + } + if err.Error() != "Access Denied" { + t.Fatal("Error") + } + resp := ToErrorResponse(err) + // valid all fields + if resp == nil { + t.Fatal("Error") + } + if resp.Code != "AccessDenied" { + t.Fatal("Error") + } + if resp.RequestID != "F19772218238A85A" { + t.Fatal("Error") + } + if resp.Message != "Access Denied" { + t.Fatal("Error") + } + if resp.Resource != "/mybucket/myphoto.jpg" { + t.Fatal("Error") + } + if resp.HostID != "GuWkjyviSiGHizehqpmsD1ndz5NClSP19DOT+s2mv7gXGQ8/X1lhbDGiIJEXpGFD" { + t.Fatal("Error") + } + if resp.XML() == "" { + t.Fatal("Error") + } +} diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/low_level_errors.go b/Godeps/_workspace/src/github.com/minio/minio-go/errors.go similarity index 59% rename from Godeps/_workspace/src/github.com/minio/minio-go/low_level_errors.go rename to Godeps/_workspace/src/github.com/minio/minio-go/errors.go index db16176a..23902583 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/low_level_errors.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/errors.go @@ -18,7 +18,7 @@ package client import ( "encoding/xml" - "net/http" + "io" ) /* **** SAMPLE ERROR RESPONSE **** @@ -32,8 +32,9 @@ import ( */ -// errorResponse is the type returned by some API operations. -type errorResponse struct { +// ErrorResponse is the type error returned by some API operations. +type ErrorResponse struct { + XMLName xml.Name `xml:"Error" json:"-"` Code string Message string Resource string @@ -41,34 +42,50 @@ type errorResponse struct { HostID string `xml:"HostId"` } +// ToErrorResponse returns parsed ErrorResponse struct, if input is nil or not ErrorResponse return value is nil +// this fuction is useful when some one wants to dig deeper into the error structures over the network. +// +// for example: +// +// import s3 "github.com/minio/minio-go" +// ... +// ... +// ..., err := s3.GetObject(...) +// if err != nil { +// resp := s3.ToErrorResponse(err) +// fmt.Println(resp.XML()) +// } +// ... +// ... +func ToErrorResponse(err error) *ErrorResponse { + switch err := err.(type) { + case ErrorResponse: + return &err + default: + return nil + } +} + +// XML send raw xml marshalled as string +func (e ErrorResponse) XML() string { + b, _ := xml.Marshal(&e) + return string(b) +} + +// Error formats HTTP error string +func (e ErrorResponse) Error() string { + return e.Message +} + +/// Internal function not exposed + // responseToError returns a new encoded ErrorResponse structure -func responseToError(res *http.Response) error { - var respError errorResponse - decoder := xml.NewDecoder(res.Body) +func responseToError(body io.Reader) error { + var respError ErrorResponse + decoder := xml.NewDecoder(body) err := decoder.Decode(&respError) if err != nil { return err } return respError } - -// GetCode get underlying error code for example: "MissingSecurityHeader" -func (e errorResponse) GetCode() string { - return e.Code -} - -// GetRequestID get request id useful in debugging -func (e errorResponse) GetRequestID() string { - return e.RequestID -} - -// XML send raw xml marshalled as string -func (e errorResponse) XML() string { - b, _ := xml.Marshal(&e) - return string(b) -} - -// Error formats HTTP error string -func (e errorResponse) Error() string { - return e.Message -} diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/low_level_api.go b/Godeps/_workspace/src/github.com/minio/minio-go/low_level_api.go index ec32a152..5807eca5 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/low_level_api.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/low_level_api.go @@ -112,7 +112,7 @@ func (a *lowLevelAPI) putBucket(bucket, acl, location string) error { defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return responseToError(resp) + return responseToError(resp.Body) } } return nil @@ -146,7 +146,7 @@ func (a *lowLevelAPI) putBucketACL(bucket, acl string) error { defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return responseToError(resp) + return responseToError(resp.Body) } } return nil @@ -179,7 +179,7 @@ func (a *lowLevelAPI) getBucketACL(bucket string) (*accessControlPolicy, error) defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return nil, responseToError(resp) + return nil, responseToError(resp.Body) } } policy := new(accessControlPolicy) @@ -218,7 +218,7 @@ func (a *lowLevelAPI) getBucketLocation(bucket string) (string, error) { defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return "", responseToError(resp) + return "", responseToError(resp.Body) } } var locationConstraint string @@ -299,7 +299,7 @@ func (a *lowLevelAPI) listObjects(bucket, marker, prefix, delimiter string, maxk defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return nil, responseToError(resp) + return nil, responseToError(resp.Body) } } listBucketResult := new(listBucketResult) @@ -417,7 +417,7 @@ func (a *lowLevelAPI) putObject(bucket, object string, size int64, body io.ReadS defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return nil, responseToError(resp) + return nil, responseToError(resp.Body) } } metadata := new(ObjectStat) @@ -469,7 +469,7 @@ func (a *lowLevelAPI) getObject(bucket, object string, offset, length uint64) (i case http.StatusOK: case http.StatusPartialContent: default: - return nil, nil, responseToError(resp) + return nil, nil, responseToError(resp.Body) } } md5sum := strings.Trim(resp.Header.Get("ETag"), "\"") // trim off the odd double quotes @@ -602,7 +602,7 @@ func (a *lowLevelAPI) listBuckets() (*listAllMyBucketsResult, error) { return nil, fmt.Errorf("%s", resp.Status) } if resp.StatusCode != http.StatusOK { - return nil, responseToError(resp) + return nil, responseToError(resp.Body) } } listAllMyBucketsResult := new(listAllMyBucketsResult) diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/low_level_multipart_api.go b/Godeps/_workspace/src/github.com/minio/minio-go/low_level_multipart_api.go index 8920e6a2..d6b7d5a5 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/low_level_multipart_api.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/low_level_multipart_api.go @@ -86,7 +86,7 @@ func (a *lowLevelAPI) listMultipartUploads(bucket, keymarker, uploadIDMarker, pr defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return nil, responseToError(resp) + return nil, responseToError(resp.Body) } } listMultipartUploadsResult := new(listMultipartUploadsResult) @@ -127,7 +127,7 @@ func (a *lowLevelAPI) initiateMultipartUpload(bucket, object string) (*initiateM defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return nil, responseToError(resp) + return nil, responseToError(resp.Body) } } initiateMultipartUploadResult := new(initiateMultipartUploadResult) @@ -176,7 +176,7 @@ func (a *lowLevelAPI) completeMultipartUpload(bucket, object, uploadID string, c defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return nil, responseToError(resp) + return nil, responseToError(resp.Body) } } completeMultipartUploadResult := new(completeMultipartUploadResult) @@ -262,7 +262,7 @@ func (a *lowLevelAPI) listObjectParts(bucket, object, uploadID string, partNumbe defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return nil, responseToError(resp) + return nil, responseToError(resp.Body) } } listObjectPartsResult := new(listObjectPartsResult) @@ -322,7 +322,7 @@ func (a *lowLevelAPI) uploadPart(bucket, object, uploadID string, partNumber int defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return nil, responseToError(resp) + return nil, responseToError(resp.Body) } } return completePart, nil