From b2a2b5478bb093745a7e23306191ee1ad78de1ab Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Tue, 11 Aug 2020 17:34:06 +0300 Subject: [PATCH] Refactoring API - api.Error should implement error interface - add method that encodes the response into ResponseWriter Signed-off-by: Evgeniy Kulikov --- api/errors.go | 4 ++++ api/response.go | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/api/errors.go b/api/errors.go index c011533..8690614 100644 --- a/api/errors.go +++ b/api/errors.go @@ -1612,6 +1612,10 @@ func (e errorCodeMap) ToAPIErr(errCode ErrorCode) Error { return e.ToAPIErrWithErr(errCode, nil) } +func (e Error) Error() string { + return fmt.Sprintf("%s: %d => %s", e.Code, e.HTTPStatusCode, e.Description) +} + // GetAPIError provides API Error for input API error code. func GetAPIError(code ErrorCode) Error { if apiErr, ok := errorCodes[code]; ok { diff --git a/api/response.go b/api/response.go index 2abc582..0e42462 100644 --- a/api/response.go +++ b/api/response.go @@ -68,7 +68,11 @@ const ( hdrSSECopyKey = "X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" ) -var deploymentID, _ = uuid.NewRandom() +var ( + deploymentID, _ = uuid.NewRandom() + + xmlHeader = []byte(xml.Header) +) // Non exhaustive list of AWS S3 standard error responses - // http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html @@ -181,6 +185,14 @@ func EncodeResponse(response interface{}) []byte { return bytesBuffer.Bytes() } +// EncodeToResponse encodes the response into ResponseWriter. +func EncodeToResponse(w http.ResponseWriter, response interface{}) error { + if _, err := w.Write(xmlHeader); err != nil { + return err + } + return xml.NewEncoder(w).Encode(response) +} + // WriteSuccessResponseXML writes success headers and response if any, // with content-type set to `application/xml`. func WriteSuccessResponseXML(w http.ResponseWriter, response []byte) {