forked from TrueCloudLab/frostfs-s3-gw
Refactoring WriteErrorResponse method
- should receive error instead custom type - change visibility of WriteResponse method Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
parent
cd2cca8fb3
commit
f3b202700e
1 changed files with 17 additions and 13 deletions
|
@ -118,8 +118,11 @@ var s3ErrorResponseMap = map[string]string{
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteErrorResponse writes error headers
|
// WriteErrorResponse writes error headers
|
||||||
func WriteErrorResponse(ctx context.Context, w http.ResponseWriter, err Error, reqURL *url.URL) {
|
func WriteErrorResponse(ctx context.Context, w http.ResponseWriter, err error, reqURL *url.URL) {
|
||||||
switch err.Code {
|
code := http.StatusBadRequest
|
||||||
|
|
||||||
|
if e, ok := err.(Error); ok {
|
||||||
|
switch e.Code {
|
||||||
case "SlowDown", "XNeoFSServerNotInitialized", "XNeoFSReadQuorum", "XNeoFSWriteQuorum":
|
case "SlowDown", "XNeoFSServerNotInitialized", "XNeoFSReadQuorum", "XNeoFSWriteQuorum":
|
||||||
// Set retry-after header to indicate user-agents to retry request after 120secs.
|
// Set retry-after header to indicate user-agents to retry request after 120secs.
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
|
||||||
|
@ -127,12 +130,13 @@ func WriteErrorResponse(ctx context.Context, w http.ResponseWriter, err Error, r
|
||||||
case "AccessDenied":
|
case "AccessDenied":
|
||||||
// TODO process when the request is from browser and also if browser
|
// TODO process when the request is from browser and also if browser
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Generate error response.
|
// Generate error response.
|
||||||
errorResponse := getAPIErrorResponse(ctx, err, reqURL.Path,
|
errorResponse := getAPIErrorResponse(ctx, err, reqURL.Path,
|
||||||
w.Header().Get(hdrAmzRequestID), deploymentID.String())
|
w.Header().Get(hdrAmzRequestID), deploymentID.String())
|
||||||
encodedErrorResponse := EncodeResponse(errorResponse)
|
encodedErrorResponse := EncodeResponse(errorResponse)
|
||||||
writeResponse(w, err.HTTPStatusCode, encodedErrorResponse, mimeXML)
|
WriteResponse(w, code, encodedErrorResponse, MimeXML)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If none of the http routes match respond with appropriate errors
|
// If none of the http routes match respond with appropriate errors
|
||||||
|
@ -162,9 +166,9 @@ func removeSensitiveHeaders(h http.Header) {
|
||||||
h.Del(hdrSSECopyKey)
|
h.Del(hdrSSECopyKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeResponse(w http.ResponseWriter, statusCode int, response []byte, mType mimeType) {
|
func WriteResponse(w http.ResponseWriter, statusCode int, response []byte, mType mimeType) {
|
||||||
setCommonHeaders(w)
|
setCommonHeaders(w)
|
||||||
if mType != mimeNone {
|
if mType != MimeNone {
|
||||||
w.Header().Set(hdrContentType, string(mType))
|
w.Header().Set(hdrContentType, string(mType))
|
||||||
}
|
}
|
||||||
w.Header().Set(hdrContentLength, strconv.Itoa(len(response)))
|
w.Header().Set(hdrContentLength, strconv.Itoa(len(response)))
|
||||||
|
@ -196,11 +200,11 @@ func EncodeToResponse(w http.ResponseWriter, response interface{}) error {
|
||||||
// WriteSuccessResponseXML writes success headers and response if any,
|
// WriteSuccessResponseXML writes success headers and response if any,
|
||||||
// with content-type set to `application/xml`.
|
// with content-type set to `application/xml`.
|
||||||
func WriteSuccessResponseXML(w http.ResponseWriter, response []byte) {
|
func WriteSuccessResponseXML(w http.ResponseWriter, response []byte) {
|
||||||
writeResponse(w, http.StatusOK, response, mimeXML)
|
WriteResponse(w, http.StatusOK, response, MimeXML)
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteSuccessResponseHeadersOnly(w http.ResponseWriter) {
|
func WriteSuccessResponseHeadersOnly(w http.ResponseWriter) {
|
||||||
writeResponse(w, http.StatusOK, nil, mimeNone)
|
WriteResponse(w, http.StatusOK, nil, MimeNone)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error - Returns S3 error string.
|
// Error - Returns S3 error string.
|
||||||
|
|
Loading…
Reference in a new issue