package customizations

import (
	
	
	
	
	
	
	

	
	smithyxml 
	
	smithyhttp 
)

// HandleResponseErrorWith200Status check for S3 200 error response.
// If an s3 200 error is found, status code for the response is modified temporarily to
// 5xx response status code.
func ( *middleware.Stack) error {
	return .Deserialize.Insert(&processResponseFor200ErrorMiddleware{}, "OperationDeserializer", middleware.After)
}

// middleware to process raw response and look for error response with 200 status code
type processResponseFor200ErrorMiddleware struct{}

// ID returns the middleware ID.
func (*processResponseFor200ErrorMiddleware) () string {
	return "S3:ProcessResponseFor200Error"
}

func ( *processResponseFor200ErrorMiddleware) (
	 context.Context,  middleware.DeserializeInput,  middleware.DeserializeHandler) (
	 middleware.DeserializeOutput,  middleware.Metadata,  error,
) {
	, ,  = .HandleDeserialize(, )
	if  != nil {
		return , , 
	}

	,  := .RawResponse.(*smithyhttp.Response)
	if ! {
		return , , &smithy.DeserializationError{Err: fmt.Errorf("unknown transport type %T", .RawResponse)}
	}

	// check if response status code is 2xx.
	if .StatusCode < 200 || .StatusCode >= 300 {
		return
	}

	var  bytes.Buffer
	 := io.TeeReader(.Body, &)

	 := xml.NewDecoder()
	,  := smithyxml.FetchRootElement()
	if  == io.EOF {
		return , , &smithy.DeserializationError{
			Err: fmt.Errorf("received empty response payload"),
		}
	}

	// rewind response body
	.Body = ioutil.NopCloser(io.MultiReader(&, .Body))

	// if start tag is "Error", the response is consider error response.
	if strings.EqualFold(.Name.Local, "Error") {
		// according to https://aws.amazon.com/premiumsupport/knowledge-center/s3-resolve-200-internalerror/
		// 200 error responses are similar to 5xx errors.
		.StatusCode = 500
	}

	return , , 
}