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

    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:
                        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)


# Traceback (KeyError)
2017-10-11T05:49:47:INFO:werkzeug: 192.168.99.1 - - [11/Oct/2017 05:49:47] "GET / HTTP/1.1" 200 -
2017-10-11T05:49:47:INFO:werkzeug: 192.168.99.1 - - [11/Oct/2017 05:49:47] "GET //192.168.99.103:8080/swagger.json HTTP/1.1" 200 -
2017-10-11T05:49:48:INFO:werkzeug: 192.168.99.1 - - [11/Oct/2017 05:49:48] "GET /img/localstack_icon.png HTTP/1.1" 200 -
2017-10-11T05:49:49:INFO:werkzeug: 192.168.99.1 - - [11/Oct/2017 05:49:49] "POST /graph HTTP/1.1" 500 -
Traceback (most recent call last):
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/code/localstack/localstack/dashboard/api.py", line 37, in get_graph
graph = infra.get_graph(name_filter=data['nameFilter'], env=env)
File "/opt/code/localstack/localstack/dashboard/infra.py", line 432, in get_graph
domains = get_elasticsearch_domains(name_filter, pool=pool, env=env)
File "/opt/code/localstack/localstack/dashboard/infra.py", line 312, in get_elasticsearch_domains
parallelize(handle, out['DomainNames'])
File "/opt/code/localstack/localstack/utils/common.py", line 698, in parallelize
result = pool.map(func, list)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get
raise self._value
KeyError: 'Endpoint'

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

    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:
                        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":
            if response.status_code >= 400 and response.status_code < 500:
                return response
            # 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])