# Buggy code
def get_existing_network(self):
    networks = self.client.networks(names=[self.parameters.network_name])
    # check if a user is trying to find network by its Id
    if not networks:
        networks = self.client.networks(ids=[self.parameters.network_name])
    if not networks:
        return None
    else:
        return networks[0]


# Traceback (docker.errors.APIError)
Traceback (most recent call last):
File "/tmp/ansible_Ao0G0J/ansible_module_docker_network.py", line 393, in <module>
main()
File "/tmp/ansible_Ao0G0J/ansible_module_docker_network.py", line 388, in main
cm = DockerNetworkManager(client)
File "/tmp/ansible_Ao0G0J/ansible_module_docker_network.py", line 206, in __init__
self.present()
File "/tmp/ansible_Ao0G0J/ansible_module_docker_network.py", line 353, in present
self.connect_containers()
File "/tmp/ansible_Ao0G0J/ansible_module_docker_network.py", line 314, in connect_containers
self.client.connect_container_to_network(name, self.parameters.network_name)
File "/usr/local/lib/python2.7/dist-packages/docker/utils/decorators.py", line 21, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/docker/utils/decorators.py", line 35, in wrapper
return f(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/docker/api/network.py", line 96, in connect_container_to_network
self._raise_for_status(res)
File "/usr/local/lib/python2.7/dist-packages/docker/client.py", line 174, in _raise_for_status
raise errors.APIError(e, response, explanation=explanation)
docker.errors.APIError: 403 Client Error: Forbidden ("{"message":"endpoint with name machine1 already exists in network myNetwork"}")

fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_Ao0G0J/ansible_module_docker_network.py\", line 393, in <mod
ule>\n    main()\n  File \"/tmp/ansible_Ao0G0J/ansible_module_docker_network.py\", line 388, in main\n    cm = DockerNetworkManager(client)\
n  File \"/tmp/ansible_Ao0G0J/ansible_module_docker_network.py\", line 206, in __init__\n    self.present()\n  File \"/tmp/ansible_Ao0G0J/an
sible_module_docker_network.py\", line 353, in present\n    self.connect_containers()\n  File \"/tmp/ansible_Ao0G0J/ansible_module_docker_ne
twork.py\", line 314, in connect_containers\n    self.client.connect_container_to_network(name, self.parameters.network_name)\n  File \"/usr
/local/lib/python2.7/dist-packages/docker/utils/decorators.py\", line 21, in wrapped\n    return f(self, resource_id, *args, **kwargs)\n  Fi
le \"/usr/local/lib/python2.7/dist-packages/docker/utils/decorators.py\", line 35, in wrapper\n    return f(self, *args, **kwargs)\n  File \
"/usr/local/lib/python2.7/dist-packages/docker/api/network.py\", line 96, in connect_container_to_network\n    self._raise_for_status(res)\n
File \"/usr/local/lib/python2.7/dist-packages/docker/client.py\", line 174, in _raise_for_status\n    raise errors.APIError(e, response, e
xplanation=explanation)\ndocker.errors.APIError: 403 Client Error: Forbidden (\"{\"message\":\"endpoint with name machine1 already exists i
n network myNetwork\"}\")\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0

# Fixed code
def get_existing_network(self):
    try:
        return self.client.inspect_network(self.parameters.network_name)
    except NotFound:
        return None