httpRespToErrorResponse returns a new encoded ErrorResponse structure as error.
(resp *http.Response, bucketName, objectName string)
| 119 | // httpRespToErrorResponse returns a new encoded ErrorResponse |
| 120 | // structure as error. |
| 121 | func httpRespToErrorResponse(resp *http.Response, bucketName, objectName string) error { |
| 122 | if resp == nil { |
| 123 | msg := "Empty http response. " + reportIssue |
| 124 | return errInvalidArgument(msg) |
| 125 | } |
| 126 | |
| 127 | errResp := ErrorResponse{ |
| 128 | StatusCode: resp.StatusCode, |
| 129 | Server: resp.Header.Get("Server"), |
| 130 | } |
| 131 | |
| 132 | success := successStatus.Contains(resp.StatusCode) |
| 133 | |
| 134 | errBody, err := xmlDecodeAndBody(resp.Body, &errResp) |
| 135 | // Xml decoding failed with no body, fall back to HTTP headers. |
| 136 | if err != nil { |
| 137 | var unmarshalErr xml.UnmarshalError |
| 138 | if success && errors.As(err, &unmarshalErr) { |
| 139 | // This is a successful message so not an error response |
| 140 | // return nil, |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | switch resp.StatusCode { |
| 145 | case http.StatusNotFound: |
| 146 | if objectName == "" { |
| 147 | errResp = ErrorResponse{ |
| 148 | StatusCode: resp.StatusCode, |
| 149 | Code: NoSuchBucket, |
| 150 | Message: s3ErrorResponseMap[NoSuchBucket], |
| 151 | BucketName: bucketName, |
| 152 | } |
| 153 | } else { |
| 154 | errResp = ErrorResponse{ |
| 155 | StatusCode: resp.StatusCode, |
| 156 | Code: NoSuchKey, |
| 157 | Message: s3ErrorResponseMap[NoSuchKey], |
| 158 | BucketName: bucketName, |
| 159 | Key: objectName, |
| 160 | } |
| 161 | } |
| 162 | case http.StatusForbidden: |
| 163 | errResp = ErrorResponse{ |
| 164 | StatusCode: resp.StatusCode, |
| 165 | Code: AccessDenied, |
| 166 | Message: s3ErrorResponseMap[AccessDenied], |
| 167 | BucketName: bucketName, |
| 168 | Key: objectName, |
| 169 | } |
| 170 | case http.StatusConflict: |
| 171 | errResp = ErrorResponse{ |
| 172 | StatusCode: resp.StatusCode, |
| 173 | Code: Conflict, |
| 174 | Message: s3ErrorResponseMap[Conflict], |
| 175 | BucketName: bucketName, |
| 176 | } |
| 177 | case http.StatusPreconditionFailed: |
| 178 | errResp = ErrorResponse{ |