diff --git a/pkg/api/api_generic_handlers.go b/pkg/api/api_generic_handlers.go index d60decf4e..a9308c1d3 100644 --- a/pkg/api/api_generic_handlers.go +++ b/pkg/api/api_generic_handlers.go @@ -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) +} diff --git a/pkg/api/api_object_handlers.go b/pkg/api/api_object_handlers.go index decac4370..164e1e463 100644 --- a/pkg/api/api_object_handlers.go +++ b/pkg/api/api_object_handlers.go @@ -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) } } }