diff options
Diffstat (limited to 'vendor/github.com/minio/minio-go/v7/api-error-response.go')
-rw-r--r-- | vendor/github.com/minio/minio-go/v7/api-error-response.go | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/vendor/github.com/minio/minio-go/v7/api-error-response.go b/vendor/github.com/minio/minio-go/v7/api-error-response.go index c45c4fdc..39df7eec 100644 --- a/vendor/github.com/minio/minio-go/v7/api-error-response.go +++ b/vendor/github.com/minio/minio-go/v7/api-error-response.go @@ -18,8 +18,11 @@ package minio import ( + "bytes" "encoding/xml" "fmt" + "io" + "io/ioutil" "net/http" ) @@ -98,6 +101,19 @@ const ( reportIssue = "Please report this issue at https://github.com/minio/minio-go/issues." ) +// xmlDecodeAndBody reads the whole body up to 1MB and +// tries to XML decode it into v. +// The body that was read and any error from reading or decoding is returned. +func xmlDecodeAndBody(bodyReader io.Reader, v interface{}) ([]byte, error) { + // read the whole body (up to 1MB) + const maxBodyLength = 1 << 20 + body, err := ioutil.ReadAll(io.LimitReader(bodyReader, maxBodyLength)) + if err != nil { + return nil, err + } + return bytes.TrimSpace(body), xmlDecoder(bytes.NewReader(body), v) +} + // httpRespToErrorResponse returns a new encoded ErrorResponse // structure as error. func httpRespToErrorResponse(resp *http.Response, bucketName, objectName string) error { @@ -111,7 +127,7 @@ func httpRespToErrorResponse(resp *http.Response, bucketName, objectName string) Server: resp.Header.Get("Server"), } - err := xmlDecoder(resp.Body, &errResp) + errBody, err := xmlDecodeAndBody(resp.Body, &errResp) // Xml decoding failed with no body, fall back to HTTP headers. if err != nil { switch resp.StatusCode { @@ -156,10 +172,17 @@ func httpRespToErrorResponse(resp *http.Response, bucketName, objectName string) Key: objectName, } default: + msg := resp.Status + if len(errBody) > 0 { + msg = string(errBody) + if len(msg) > 1024 { + msg = msg[:1024] + "..." + } + } errResp = ErrorResponse{ StatusCode: resp.StatusCode, Code: resp.Status, - Message: resp.Status, + Message: msg, BucketName: bucketName, } } |