# Buggy code
def update_cloudformation(
    method, path, data, headers, response=None, return_forward_info=False
):
    req_data = None
    if method == "POST" and path == "/":
        req_data = urlparse.parse_qs(data)
        action = req_data.get("Action")[0]

    if return_forward_info:
        if req_data:
            if action == "CreateChangeSet":
                return create_change_set(req_data)
            elif action == "DescribeChangeSet":
                return describe_change_set(req_data)
            elif action == "ExecuteChangeSet":
                return execute_change_set(req_data)
        return True

    if req_data:
        if action == "DescribeStackResources":
            response_dict = xmltodict.parse(response.content)[
                "DescribeStackResourcesResponse"
            ]
            resources = response_dict["DescribeStackResourcesResult"]["StackResources"]
            if not resources:
                # TODO: check if stack exists
                return error_response("Stack does not exist", code=404)
        if action == "DescribeStackResource":
            if response.status_code >= 500:
                # fix an error in moto where it fails with 500 if the stack does not exist
                return error_response("Stack resource does not exist", code=404)
        elif action == "CreateStack" or action == "UpdateStack":
            if response.status_code in range(200, 300):
                # run the actual deployment
                template = template_deployer.template_to_json(
                    req_data.get("TemplateBody")[0]
                )
                template_deployer.deploy_template(template)


# Traceback (ResponseParserError)
ERROR:localstack.mock.generic_proxy:Error forwarding request: Traceback (most recent call last):
File "/opt/code/localstack/localstack/mock/generic_proxy.py", line 88, in forward
data=data, headers=self.headers, return_forward_info=True)
File "/opt/code/localstack/localstack/mock/proxy/sns_listener.py", line 37, in update_sns
queue_url = aws_stack.get_sqs_queue_url(queue_name)
File "/opt/code/localstack/localstack/utils/aws/aws_stack.py", line 285, in get_sqs_queue_url
response = client.get_queue_url(QueueName=queue_name)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/client.py", line 253, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/client.py", line 531, in _make_api_call
operation_model, request_dict)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/endpoint.py", line 141, in make_request
return self._send_request(request_dict, operation_model)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/endpoint.py", line 168, in _send_request
request, operation_model, attempts)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/endpoint.py", line 233, in _get_response
response_dict, operation_model.output_shape)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/parsers.py", line 210, in parse
parsed = self._do_error_parse(response, shape)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/parsers.py", line 438, in _do_error_parse
root = self._parse_xml_string_to_dom(xml_contents)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/parsers.py", line 390, in _parse_xml_string_to_dom
"invalid XML received:\n%s" % (e, xml_string))
ResponseParserError: Unable to parse response (syntax error: line 1, column 54), invalid XML received:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>

# Fixed code
def update_cloudformation(
    method, path, data, headers, response=None, return_forward_info=False
):
    req_data = None
    if method == "POST" and path == "/":
        req_data = urlparse.parse_qs(data)
        action = req_data.get("Action")[0]

    if return_forward_info:
        if req_data:
            if action == "CreateChangeSet":
                return create_change_set(req_data)
            elif action == "DescribeChangeSet":
                return describe_change_set(req_data)
            elif action == "ExecuteChangeSet":
                return execute_change_set(req_data)
            elif action == "UpdateStack" and req_data.get("TemplateURL"):
                # Temporary fix until the moto CF backend can handle TemplateURL (currently fails)
                url = re.sub(
                    r"https?://s3\.amazonaws\.com",
                    aws_stack.get_local_service_url("s3"),
                    req_data.get("TemplateURL")[0],
                )
                req_data["TemplateBody"] = requests.get(url).content
                modified_data = urlparse.urlencode(req_data, doseq=True)
                return Request(data=modified_data, headers=headers, method=method)
        return True

    if req_data:
        if action == "DescribeStackResources":
            if response.status_code < 300:
                response_dict = xmltodict.parse(response.content)[
                    "DescribeStackResourcesResponse"
                ]
                resources = response_dict["DescribeStackResourcesResult"][
                    "StackResources"
                ]
                if not resources:
                    # Check if stack exists
                    stack_name = req_data.get("StackName")[0]
                    cloudformation_client = aws_stack.connect_to_service(
                        "cloudformation"
                    )
                    try:
                        cloudformation_client.describe_stacks(StackName=stack_name)
                    except Exception as e:
                        return error_response(
                            "Stack with id %s does not exist" % stack_name, code=404
                        )
        if action == "DescribeStackResource":
            if response.status_code >= 500:
                # fix an error in moto where it fails with 500 if the stack does not exist
                return error_response("Stack resource does not exist", code=404)
        elif action == "CreateStack" or action == "UpdateStack":
            # run the actual deployment
            template = template_deployer.template_to_json(
                req_data.get("TemplateBody")[0]
            )
            template_deployer.deploy_template(template, req_data.get("StackName")[0])
            if response.status_code >= 400:
                return make_response(action)