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