Simplifying error handling in api bucket handlers

This commit is contained in:
Frederick F. Kautz IV 2015-03-30 14:27:32 -07:00
parent 06c329d26b
commit 62c4c997c2
2 changed files with 21 additions and 62 deletions

View File

@ -48,9 +48,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
case true: case true:
setObjectHeaders(w, metadata) setObjectHeaders(w, metadata)
if _, err := server.driver.GetObject(w, bucket, object); err != nil { if _, err := server.driver.GetObject(w, bucket, object); err != nil {
log.Error.Println(err)
// unable to write headers, we've already printed data. Just close the connection. // unable to write headers, we've already printed data. Just close the connection.
return
} }
case false: case false:
metadata.Size = httpRange.length metadata.Size = httpRange.length
@ -58,11 +56,9 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
w.WriteHeader(http.StatusPartialContent) w.WriteHeader(http.StatusPartialContent)
_, err := server.driver.GetPartialObject(w, bucket, object, httpRange.start, httpRange.length) _, err := server.driver.GetPartialObject(w, bucket, object, httpRange.start, httpRange.length)
if err != nil { if err != nil {
log.Error.Println(err)
// unable to write headers, we've already printed data. Just close the connection. // unable to write headers, we've already printed data. Just close the connection.
return log.Error.Println(err)
} }
} }
} }
case drivers.ObjectNotFound: case drivers.ObjectNotFound:
@ -132,6 +128,8 @@ func (server *minioAPI) putObjectHandler(w http.ResponseWriter, req *http.Reques
resources := getBucketResources(req.URL.Query()) resources := getBucketResources(req.URL.Query())
if resources.Policy == true && object == "" { if resources.Policy == true && object == "" {
// TODO figure out if we can hand this off to router instead of embedding here.
// hand off request to pubBucketPolicyHandler
server.putBucketPolicyHandler(w, req) server.putBucketPolicyHandler(w, req)
return return
} }

View File

@ -21,6 +21,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"
) )
@ -36,11 +37,7 @@ func (server *minioAPI) putBucketPolicyHandler(w http.ResponseWriter, req *http.
policy, ok := drivers.Parsepolicy(req.Body) policy, ok := drivers.Parsepolicy(req.Body)
if ok == false { if ok == false {
error := getErrorCode(InvalidPolicyDocument) writeErrorResponse(w, req, InvalidPolicyDocument, acceptsContentType, req.URL.Path)
errorResponse := getErrorResponse(error, bucket)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
return return
} }
@ -54,37 +51,21 @@ func (server *minioAPI) putBucketPolicyHandler(w http.ResponseWriter, req *http.
} }
case drivers.BucketNameInvalid: case drivers.BucketNameInvalid:
{ {
error := getErrorCode(InvalidBucketName) writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path)
errorResponse := getErrorResponse(error, bucket)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
} }
case drivers.BucketNotFound: case drivers.BucketNotFound:
{ {
error := getErrorCode(NoSuchBucket) writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path)
errorResponse := getErrorResponse(error, bucket)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
} }
case drivers.BackendCorrupted: case drivers.BackendCorrupted:
{ {
log.Error.Println(err) log.Error.Println(err)
error := getErrorCode(InternalError) writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
errorResponse := getErrorResponse(error, bucket)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
} }
case drivers.ImplementationError: case drivers.ImplementationError:
{ {
log.Error.Println(err) log.Error.Println(err)
error := getErrorCode(InternalError) writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
errorResponse := getErrorResponse(error, bucket)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
} }
} }
} }
@ -102,59 +83,39 @@ func (server *minioAPI) getBucketPolicyHandler(w http.ResponseWriter, req *http.
switch err := err.(type) { switch err := err.(type) {
case nil: case nil:
{ {
responsePolicy, ret := json.Marshal(p) responsePolicy, err := json.Marshal(p)
if ret != nil { if err != nil {
error := getErrorCode(InternalError) // log error
errorResponse := getErrorResponse(error, bucket) log.Error.Println(iodine.New(err, nil))
setCommonHeaders(w, getContentTypeString(acceptsContentType)) writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
w.WriteHeader(error.HTTPStatusCode) return
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
} }
setCommonHeaders(w, getContentTypeString(acceptsContentType)) setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.Header().Set("Connection", "keep-alive") w.Header().Set("Connection", "keep-alive")
w.WriteHeader(http.StatusOK)
w.Write(responsePolicy) w.Write(responsePolicy)
} }
case drivers.BucketNameInvalid: case drivers.BucketNameInvalid:
{ {
error := getErrorCode(InvalidBucketName) writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path)
errorResponse := getErrorResponse(error, bucket)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
} }
case drivers.BucketNotFound: case drivers.BucketNotFound:
{ {
error := getErrorCode(NoSuchBucket) writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path)
errorResponse := getErrorResponse(error, bucket)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
} }
case drivers.BucketPolicyNotFound: case drivers.BucketPolicyNotFound:
{ {
error := getErrorCode(NoSuchBucketPolicy) writeErrorResponse(w, req, NoSuchBucketPolicy, acceptsContentType, req.URL.Path)
errorResponse := getErrorResponse(error, bucket)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
} }
case drivers.BackendCorrupted: case drivers.BackendCorrupted:
{ {
log.Error.Println(err) log.Error.Println(err)
error := getErrorCode(InternalError) writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
errorResponse := getErrorResponse(error, bucket)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
} }
case drivers.ImplementationError: case drivers.ImplementationError:
{ {
log.Error.Println(err) log.Error.Println(err)
error := getErrorCode(InternalError) writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
errorResponse := getErrorResponse(error, bucket)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
} }
} }
} }