Merge pull request #414 from fkautz/pr_out_simplifying_error_response_writing_for_object_handlers

This commit is contained in:
Frederick F. Kautz IV 2015-03-29 22:03:24 -07:00
commit 33414915a1
2 changed files with 28 additions and 121 deletions

View File

@ -142,3 +142,14 @@ func ignoreUnImplementedObjectResources(req *http.Request) bool {
}
return false
}
func writeErrorResponse(w http.ResponseWriter, req *http.Request, errorType int, acceptsContentType contentType, resource string) {
error := getErrorCode(errorType)
errorResponse := getErrorResponse(error, resource)
// set headers
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
}

View File

@ -41,13 +41,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
{
httpRange, err := getRequestedRange(req, metadata.Size)
if err != nil {
log.Error.Println(err)
error := getErrorCode(InvalidRange)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, InvalidRange, acceptsContentType, req.URL.Path)
return
}
switch httpRange.start == 0 && httpRange.length == 0 {
@ -73,50 +67,24 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
}
case drivers.ObjectNotFound:
{
error := getErrorCode(NoSuchKey)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path)
}
case drivers.BucketNotFound:
{
error := getErrorCode(NoSuchBucket)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path)
}
case drivers.ObjectNameInvalid:
{
error := getErrorCode(NoSuchKey)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path)
}
case drivers.BucketNameInvalid:
{
error := getErrorCode(InvalidBucketName)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path)
}
default:
{
// Embed errors log on serve side
log.Error.Println(err)
error := getErrorCode(InternalError)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
}
}
}
@ -135,43 +103,19 @@ func (server *minioAPI) headObjectHandler(w http.ResponseWriter, req *http.Reque
switch err := err.(type) {
case nil:
setObjectHeaders(w, metadata)
w.WriteHeader(http.StatusOK)
case drivers.ObjectNotFound:
{
// get error
error := getErrorCode(NoSuchKey)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
// set headers
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path)
}
case drivers.ObjectNameInvalid:
{
// get error
error := getErrorCode(NoSuchKey)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
// set headers
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path)
}
case drivers.ImplementationError:
{
// Embed error log on server side
log.Error.Println(err)
// get error
error := getErrorCode(InternalError)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
// set headers
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
}
}
}
@ -199,79 +143,31 @@ func (server *minioAPI) putObjectHandler(w http.ResponseWriter, req *http.Reques
case nil:
w.Header().Set("Server", "Minio")
w.Header().Set("Connection", "close")
w.WriteHeader(http.StatusOK)
case drivers.ImplementationError:
{
// Embed error log on server side
log.Error.Println(err)
// get error
error := getErrorCode(InternalError)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
// set headers
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
}
case drivers.BucketNotFound:
{
// get error
error := getErrorCode(NoSuchBucket)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
// set headers
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path)
}
case drivers.BucketNameInvalid:
{
// get error
error := getErrorCode(InvalidBucketName)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
// set headers
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path)
}
case drivers.ObjectExists:
{
// get error
error := getErrorCode(NotImplemented)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
// set headers
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, NotImplemented, acceptsContentType, req.URL.Path)
}
case drivers.BadDigest:
{
// get error
error := getErrorCode(BadDigest)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
// set headers
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, BadDigest, acceptsContentType, req.URL.Path)
}
case drivers.InvalidDigest:
{
// get error
error := getErrorCode(InvalidDigest)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
// set headers
setCommonHeaders(w, getContentTypeString(acceptsContentType))
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
w.Write(encodedErrorResponse)
writeErrorResponse(w, req, InvalidDigest, acceptsContentType, req.URL.Path)
}
}
}