Refactoring API

- api.Error should implement error interface
- add method that encodes the response into ResponseWriter

Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
Evgeniy Kulikov 2020-08-11 17:34:06 +03:00
parent 4d4e52fea7
commit b2a2b5478b
2 changed files with 17 additions and 1 deletions

View file

@ -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 {

View file

@ -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) {