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:
setObjectHeaders(w, metadata)
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.
return
}
case false:
metadata.Size = httpRange.length
@ -58,11 +56,9 @@ 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 {
log.Error.Println(err)
// unable to write headers, we've already printed data. Just close the connection.
return
log.Error.Println(err)
}
}
}
case drivers.ObjectNotFound:
@ -132,6 +128,8 @@ func (server *minioAPI) putObjectHandler(w http.ResponseWriter, req *http.Reques
resources := getBucketResources(req.URL.Query())
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)
return
}

View File

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