Handle errors properly, also remove need for normalizing path on windows

This commit is contained in:
Harshavardhana
2015-04-22 00:17:01 -07:00
parent 629a4c1cd4
commit 0eaf7ec5b4
12 changed files with 173 additions and 134 deletions
+1 -1
View File
@@ -105,7 +105,7 @@ func (manager mcClientManager) getTargetWriter(targetURL string, targetConfig *h
if client.GetType(targetURL) != client.Filesystem {
if err := targetClnt.Stat(); err != nil {
switch iodine.ToError(err).(type) {
case client.BucketNotFound:
case s3.BucketNotFound:
err := targetClnt.PutBucket()
if err != nil {
return nil, iodine.New(err, map[string]string{"failedURL": targetURL})
+5 -81
View File
@@ -29,77 +29,6 @@ func (e APINotImplemented) Error() string {
return "API not implemented: " + e.API
}
// InvalidMaxKeys - invalid maxkeys provided
type InvalidMaxKeys struct {
MaxKeys int
}
func (e InvalidMaxKeys) Error() string {
return "invalid maxkeys: " + strconv.Itoa(e.MaxKeys)
}
// InvalidRange - invalid range requested
type InvalidRange struct {
Offset int64
}
func (e InvalidRange) Error() string {
return "invalid range offset: " + strconv.FormatInt(e.Offset, 10)
}
// GenericBucketError - generic bucket operations error
type GenericBucketError struct {
Bucket string
}
// BucketNotFound - bucket requested does not exist
type BucketNotFound GenericBucketError
func (e BucketNotFound) Error() string {
return "bucket " + e.Bucket + " not found"
}
// BucketExists - bucket exists
type BucketExists GenericBucketError
func (e BucketExists) Error() string {
return "bucket " + e.Bucket + " exists"
}
// InvalidBucketName - bucket name invalid
type InvalidBucketName GenericBucketError
func (e InvalidBucketName) Error() string {
return "Invalid bucketname " + e.Bucket
}
// GenericObjectError - generic object operations error
type GenericObjectError struct {
Bucket string
Object string
}
// ObjectNotFound - object requested does not exist
type ObjectNotFound GenericObjectError
func (e ObjectNotFound) Error() string {
return "object " + e.Object + " not found in bucket " + e.Bucket
}
// InvalidObjectName - object requested is invalid
type InvalidObjectName GenericObjectError
func (e InvalidObjectName) Error() string {
return "object " + e.Object + "at" + e.Bucket + "is invalid"
}
// ObjectExists - object exists
type ObjectExists GenericObjectError
func (e ObjectExists) Error() string {
return "object " + e.Object + " exists"
}
// GenericError - generic error
type GenericError struct {
Err error
@@ -119,16 +48,11 @@ func (e InvalidArgument) Error() string {
return e.Err.Error()
}
// InvalidAuthorizationKey - invalid authorization key
type InvalidAuthorizationKey GenericError
func (e InvalidAuthorizationKey) Error() string {
return e.Err.Error()
// InvalidRange - invalid range requested
type InvalidRange struct {
Offset int64
}
// AuthorizationKeyEmpty - empty auth key provided
type AuthorizationKeyEmpty GenericError
func (e AuthorizationKeyEmpty) Error() string {
return e.Err.Error()
func (e InvalidRange) Error() string {
return "invalid range offset: " + strconv.FormatInt(e.Offset, 10)
}
+27
View File
@@ -0,0 +1,27 @@
package fs
// GenericFileError - generic file error
type GenericFileError struct {
path string
}
// FileNotFound (ENOENT) - file not found
type FileNotFound GenericFileError
func (e FileNotFound) Error() string {
return "Requested file " + e.path + "not found"
}
// FileISDir (EISDIR) - accessed file is a directory
type FileISDir GenericFileError
func (e FileISDir) Error() string {
return "Requested file " + e.path + "is a directory"
}
// FileNotDir (ENOTDIR) - accessed file is not a directory
type FileNotDir GenericFileError
func (e FileNotDir) Error() string {
return "Requested file " + e.path + "is not a directory"
}
+2 -2
View File
@@ -41,8 +41,8 @@ func (f *fsClient) Put(md5HexString string, size int64) (io.WriteCloser, error)
blockingWriter.Release(err)
return
}
objectDir, _ := filepath.Split(f.Path)
objectPath := f.Path
objectDir, _ := filepath.Split(f.path)
objectPath := f.path
if err := os.MkdirAll(objectDir, 0700); err != nil {
err := iodine.New(err, nil)
r.CloseWithError(err)
+2 -2
View File
@@ -39,8 +39,8 @@ func (f *fsClient) Put(md5HexString string, size int64) (io.WriteCloser, error)
blockingWriter.Release(err)
return
}
objectDir, _ := filepath.Split(f.normalizedPath())
objectPath := f.normalizedPath()
objectDir, _ := filepath.Split(f.path)
objectPath := f.path
if err := os.MkdirAll(objectDir, 0700); err != nil {
err := iodine.New(err, nil)
r.CloseWithError(err)
+18 -17
View File
@@ -32,25 +32,27 @@ import (
)
type fsClient struct {
Path string
path string
}
// New - instantiate a new fs client
func New(path string) client.Client {
return &fsClient{Path: path}
if strings.TrimSpace(path) == "" {
return nil
}
return &fsClient{path: path}
}
/// Object operations
// getObjectMetadata - wrapper function to get file stat
func (f *fsClient) getObjectMetadata() (os.FileInfo, error) {
st, err := os.Stat(f.Path)
/* FIXME: after rewritng client.errors to handle generic path and URLs, fix the error messages appropriately.
st, err := os.Stat(f.path)
if os.IsNotExist(err) {
return nil, iodine.New(client.ObjectNotFound{Bucket: bucket, Object: object}, nil)
return nil, iodine.New(FileNotFound{path: f.path}, nil)
}
if st.IsDir() {
return nil, iodine.New(client.InvalidObjectName{Bucket: bucket, Object: object}, nil)
}*/
return nil, iodine.New(FileISDir{path: f.path}, nil)
}
if err != nil {
return nil, iodine.New(err, nil)
}
@@ -63,7 +65,7 @@ func (f *fsClient) Get() (body io.ReadCloser, size int64, md5 string, err error)
if err != nil {
return nil, 0, "", iodine.New(err, nil)
}
body, err = os.Open(f.Path)
body, err = os.Open(f.path)
if err != nil {
return nil, 0, "", iodine.New(err, nil)
}
@@ -81,7 +83,7 @@ func (f *fsClient) GetPartial(offset, length int64) (body io.ReadCloser, size in
if err != nil {
return nil, 0, "", iodine.New(err, nil)
}
body, err = os.Open(f.Path)
body, err = os.Open(f.path)
if err != nil {
return nil, 0, "", iodine.New(err, nil)
}
@@ -120,27 +122,26 @@ func (f *fsClient) List() (items []*client.Item, err error) {
if fi.IsDir() {
return nil // not a fs skip
}
// trim f.Path
// trim f.path
item := &client.Item{
Name: strings.TrimPrefix(fp, f.Path+string(filepath.Separator)),
Name: strings.TrimPrefix(fp, f.path+string(filepath.Separator)),
Time: fi.ModTime(),
Size: fi.Size(),
}
items = append(items, item)
return nil
}
err = filepath.Walk(f.Path, visitFS)
err = filepath.Walk(f.path, visitFS)
if err != nil {
return nil, iodine.New(err, nil)
}
sort.Sort(client.BySize(items))
return items, nil
}
// PutBucket - create a new bucket
func (f *fsClient) PutBucket() error {
err := os.MkdirAll(f.Path, 0700)
err := os.MkdirAll(f.path, 0700)
if err != nil {
return iodine.New(err, nil)
}
@@ -149,12 +150,12 @@ func (f *fsClient) PutBucket() error {
// Stat -
func (f *fsClient) Stat() error {
st, err := os.Stat(f.Path)
st, err := os.Stat(f.path)
if os.IsNotExist(err) {
return iodine.New(client.BucketNotFound{Bucket: ""}, nil)
return iodine.New(err, nil)
}
if !st.IsDir() {
return iodine.New(client.InvalidBucketName{Bucket: ""}, nil)
return iodine.New(FileNotDir{path: f.path}, nil)
}
return nil
}
+18 -16
View File
@@ -30,24 +30,26 @@ import (
)
type fsClient struct {
Path string
path string
}
// New - instantiate a new fs client
func New(path string) client.Client {
return &fsClient{Path: path}
if strings.TrimSpace(path) == "" {
return nil
}
return &fsClient{path: path}
}
// getObjectMetadata - wrapper function to get file stat
func (f *fsClient) getObjectMetadata() (os.FileInfo, error) {
st, err := os.Stat(filepath.Clean(f.Path))
/* FIXME: after rewritng client.errors to handle generic path and URLs, fix the error messages appropriately.
st, err := os.Stat(filepath.Clean(f.path))
if os.IsNotExist(err) {
return nil, iodine.New(client.ObjectNotFound{Bucket: bucket, Object: object}, nil)
return nil, iodine.New(FileNotFound{Path: f.path}, nil)
}
if st.IsDir() {
return nil, iodine.New(client.InvalidObjectName{Bucket: bucket, Object: object}, nil)
}*/
return nil, iodine.New(FileISDir{Path: f.path}, nil)
}
if err != nil {
return nil, iodine.New(err, nil)
}
@@ -60,7 +62,7 @@ func (f *fsClient) Get() (body io.ReadCloser, size int64, md5 string, err error)
if err != nil {
return nil, 0, "", iodine.New(err, nil)
}
body, err = os.Open(f.normalizedPath())
body, err = os.Open(f.path)
if err != nil {
return nil, 0, "", iodine.New(err, nil)
}
@@ -78,7 +80,7 @@ func (f *fsClient) GetPartial(offset, length int64) (body io.ReadCloser, size in
if err != nil {
return nil, 0, "", iodine.New(err, nil)
}
body, err = os.Open(f.normalizedPath())
body, err = os.Open(f.path)
if err != nil {
return nil, 0, "", iodine.New(err, nil)
}
@@ -109,7 +111,7 @@ func (f *fsClient) GetObjectMetadata() (item *client.Item, reterr error) {
// listBuckets - get list of buckets
func (f *fsClient) listBuckets() ([]*client.Item, error) {
buckets, err := ioutil.ReadDir(f.normalizedPath())
buckets, err := ioutil.ReadDir(f.path)
if err != nil {
return nil, iodine.New(err, nil)
}
@@ -143,14 +145,14 @@ func (f *fsClient) List() (items []*client.Item, err error) {
}
// trim f.String()
item := &client.Item{
Name: strings.TrimPrefix(filepath.Clean(fp), f.normalizedPath()+string(filepath.Separator)),
Name: strings.TrimPrefix(filepath.Clean(fp), f.path+string(filepath.Separator)),
Time: fi.ModTime(),
Size: fi.Size(),
}
items = append(items, item)
return nil
}
err = filepath.Walk(f.normalizedPath(), visitFS)
err = filepath.Walk(f.path, visitFS)
if err != nil {
return nil, iodine.New(err, nil)
}
@@ -161,7 +163,7 @@ func (f *fsClient) List() (items []*client.Item, err error) {
// PutBucket - create a new bucket
func (f *fsClient) PutBucket() error {
err := os.MkdirAll(f.normalizedPath(), 0700)
err := os.MkdirAll(f.path, 0700)
if err != nil {
return iodine.New(err, nil)
}
@@ -170,12 +172,12 @@ func (f *fsClient) PutBucket() error {
// Stat -
func (f *fsClient) Stat() error {
st, err := os.Stat(f.normalizedPath())
st, err := os.Stat(f.path)
if os.IsNotExist(err) {
return iodine.New(client.BucketNotFound{Bucket: f.normalizedPath()}, nil)
return iodine.New(err, nil)
}
if !st.IsDir() {
return iodine.New(client.InvalidBucketName{Bucket: f.normalizedPath()}, nil)
return iodine.New(FileNotDir{Path: f.path}, nil)
}
return nil
}
+7 -6
View File
@@ -53,8 +53,8 @@ import (
/// Bucket API operations
// ListBuckets - Get list of buckets
func (c *s3Client) listBuckets() ([]*client.Item, error) {
// Get list of buckets
func (c *s3Client) listBucketsInternal() ([]*client.Item, error) {
var res *http.Response
var err error
@@ -88,7 +88,8 @@ func (c *s3Client) listBuckets() ([]*client.Item, error) {
}
var buckets allMyBuckets
if err := xml.NewDecoder(res.Body).Decode(&buckets); err != nil {
return nil, iodine.New(client.UnexpectedError{Err: errors.New("Malformed response received from server")},
return nil, iodine.New(client.UnexpectedError{
Err: errors.New("Malformed response received from server")},
map[string]string{"XMLError": err.Error()})
}
var items []*client.Item
@@ -105,7 +106,7 @@ func (c *s3Client) listBuckets() ([]*client.Item, error) {
func (c *s3Client) PutBucket() error {
bucket, _ := c.url2Object()
if !client.IsValidBucketName(bucket) || strings.Contains(bucket, ".") {
return iodine.New(client.InvalidBucketName{Bucket: bucket}, nil)
return iodine.New(InvalidBucketName{Bucket: bucket}, nil)
}
u := fmt.Sprintf("%s://%s/%s", c.Scheme, c.Host, bucket)
req, err := c.getNewReq(u, nil)
@@ -132,7 +133,7 @@ func (c *s3Client) PutBucket() error {
func (c *s3Client) Stat() error {
bucket, _ := c.url2Object()
if !client.IsValidBucketName(bucket) || strings.Contains(bucket, ".") {
return iodine.New(client.InvalidBucketName{Bucket: bucket}, nil)
return iodine.New(InvalidBucketName{Bucket: bucket}, nil)
}
u := fmt.Sprintf("%s://%s/%s", c.Scheme, c.Host, bucket)
req, err := c.getNewReq(u, nil)
@@ -150,7 +151,7 @@ func (c *s3Client) Stat() error {
switch res.StatusCode {
case http.StatusNotFound:
return iodine.New(client.BucketNotFound{Bucket: bucket}, nil)
return iodine.New(BucketNotFound{Bucket: bucket}, nil)
case http.StatusOK:
fallthrough
case http.StatusMovedPermanently:
+8 -5
View File
@@ -41,8 +41,9 @@ func (c *s3Client) List() (items []*client.Item, err error) {
items = append(items, item)
return items, nil
default:
// if not bucket provided return list of all buckets
if bucket == "" {
return c.listBuckets()
return c.listBucketsInternal()
}
// List all objects matching the key prefix
items, err = c.listObjectsInternal(bucket, "", objectPrefix, "", globalMaxKeys)
@@ -109,7 +110,7 @@ func (c *s3Client) listObjectsInternal(bucket string, startAt, prefix, delimiter
var urlReq string
var buffer bytes.Buffer
if maxKeys <= 0 {
return nil, iodine.New(client.InvalidMaxKeys{MaxKeys: maxKeys}, nil)
return nil, iodine.New(InvalidMaxKeys{MaxKeys: maxKeys}, nil)
}
marker := startAt
for len(items) < maxKeys {
@@ -137,8 +138,8 @@ func (c *s3Client) listObjectsInternal(bucket string, startAt, prefix, delimiter
}
if bres.MaxKeys != fetchN || bres.Name != bucket || bres.Marker != marker {
msg := fmt.Sprintf("Unexpected parse from server: %#v", bres)
err = errors.New(msg)
return nil, iodine.New(err, nil)
return nil, iodine.New(client.UnexpectedError{
Err: errors.New(msg)}, nil)
}
items, marker, err = c.filterItems(startAt, marker, bres.Contents)
if err != nil {
@@ -149,7 +150,9 @@ func (c *s3Client) listObjectsInternal(bucket string, startAt, prefix, delimiter
}
if len(items) == 0 {
return nil, iodine.New(errors.New("No items replied"), nil)
errMsg := errors.New("No items replied")
return nil, iodine.New(client.UnexpectedError{
Err: errMsg}, nil)
}
}
sort.Sort(client.BySize(items))
+82
View File
@@ -19,11 +19,93 @@ package s3
import (
"fmt"
"net/http"
"strconv"
"github.com/clbanning/mxj"
"github.com/minio-io/minio/pkg/iodine"
)
// InvalidMaxKeys - invalid maxkeys provided
type InvalidMaxKeys struct {
MaxKeys int
}
func (e InvalidMaxKeys) Error() string {
return "invalid maxkeys: " + strconv.Itoa(e.MaxKeys)
}
// GenericBucketError - generic bucket operations error
type GenericBucketError struct {
Bucket string
}
// BucketNotFound - bucket requested does not exist
type BucketNotFound GenericBucketError
func (e BucketNotFound) Error() string {
return "bucket " + e.Bucket + " not found"
}
// BucketExists - bucket exists
type BucketExists GenericBucketError
func (e BucketExists) Error() string {
return "bucket " + e.Bucket + " exists"
}
// InvalidBucketName - bucket name invalid
type InvalidBucketName GenericBucketError
func (e InvalidBucketName) Error() string {
return "Invalid bucketname " + e.Bucket
}
// GenericObjectError - generic object operations error
type GenericObjectError struct {
Bucket string
Object string
}
// ObjectNotFound - object requested does not exist
type ObjectNotFound GenericObjectError
func (e ObjectNotFound) Error() string {
return "object " + e.Object + " not found in bucket " + e.Bucket
}
// InvalidObjectName - object requested is invalid
type InvalidObjectName GenericObjectError
func (e InvalidObjectName) Error() string {
return "object " + e.Object + "at" + e.Bucket + "is invalid"
}
// ObjectExists - object exists
type ObjectExists GenericObjectError
func (e ObjectExists) Error() string {
return "object " + e.Object + " exists"
}
// GenericError - generic error
type GenericError struct {
Err error
}
// InvalidAuthorizationKey - invalid authorization key
type InvalidAuthorizationKey GenericError
func (e InvalidAuthorizationKey) Error() string {
return e.Err.Error()
}
// AuthorizationKeyEmpty - empty auth key provided
type AuthorizationKeyEmpty GenericError
func (e AuthorizationKeyEmpty) Error() string {
return e.Err.Error()
}
/* **** SAMPLE ERROR RESPONSE ****
s3.ListBucket: status 403:
<?xml version="1.0" encoding="UTF-8"?>
+1 -1
View File
@@ -70,7 +70,7 @@ func (c *s3Client) GetObjectMetadata() (item *client.Item, reterr error) {
defer res.Body.Close()
switch res.StatusCode {
case http.StatusNotFound:
return nil, iodine.New(client.ObjectNotFound{Bucket: bucket, Object: object}, nil)
return nil, iodine.New(ObjectNotFound{Bucket: bucket, Object: object}, nil)
case http.StatusOK:
contentLength, err := strconv.ParseInt(res.Header.Get("Content-Length"), 10, 64)
if err != nil {
+2 -3
View File
@@ -21,7 +21,6 @@ import (
"net/http"
"strings"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/minio/pkg/iodine"
)
@@ -57,10 +56,10 @@ Example AWS S3 Request / Response
// HTTP S3 request validator.
func (t s3Verify) Request(req *http.Request) error {
if req.Header.Get("Authorization") == "" {
return iodine.New(client.AuthorizationKeyEmpty{Err: errors.New("authorization key empty")}, nil)
return iodine.New(AuthorizationKeyEmpty{Err: errors.New("authorization key empty")}, nil)
}
if !strings.HasPrefix(req.Header.Get("Authorization"), "AWS") {
return iodine.New(client.InvalidAuthorizationKey{Err: errors.New("invalid authorization key")}, nil)
return iodine.New(InvalidAuthorizationKey{Err: errors.New("invalid authorization key")}, nil)
}
return nil
}