Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception handling #121

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/121-exception-handling.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "all modules - use ``to_native`` to convert exceptions to strings (https://github.com/ansible-collections/community.docker/pull/121)."
24 changes: 14 additions & 10 deletions plugins/modules/docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@
HAS_COMPOSE_EXC = traceback.format_exc()
DEFAULT_TIMEOUT = 10

from ansible.module_utils._text import to_native

from ansible_collections.community.docker.plugins.module_utils.common import (
AnsibleDockerClient,
DockerBaseClass,
Expand Down Expand Up @@ -632,7 +634,7 @@ def __init__(self, client):

if not HAS_COMPOSE:
self.client.fail("Unable to load docker-compose. Try `pip install docker-compose`. Error: %s" %
HAS_COMPOSE_EXC)
to_native(HAS_COMPOSE_EXC))

if LooseVersion(compose_version) < LooseVersion(MINIMUM_COMPOSE_VERSION):
self.client.fail("Found docker-compose version %s. Minimum required version is %s. "
Expand All @@ -647,7 +649,7 @@ def __init__(self, client):

if self.definition:
if not HAS_YAML:
self.client.fail("Unable to load yaml. Try `pip install PyYAML`. Error: %s" % HAS_YAML_EXC)
self.client.fail("Unable to load yaml. Try `pip install PyYAML`. Error: %s" % to_native(HAS_YAML_EXC))

if not self.project_name:
self.client.fail("Parameter error - project_name required when providing definition.")
Expand All @@ -660,7 +662,7 @@ def __init__(self, client):
with open(compose_file, 'w') as f:
f.write(yaml.dump(self.definition, default_flow_style=False))
except Exception as exc:
self.client.fail("Error writing to %s - %s" % (compose_file, str(exc)))
self.client.fail("Error writing to %s - %s" % (compose_file, to_native(exc)))
else:
if not self.project_src:
self.client.fail("Parameter error - project_src required.")
Expand All @@ -669,7 +671,7 @@ def __init__(self, client):
self.log("project_src: %s" % self.project_src)
self.project = project_from_options(self.project_src, self.options)
except Exception as exc:
self.client.fail("Configuration error - %s" % str(exc))
self.client.fail("Configuration error - %s" % to_native(exc))

def exec_module(self):
result = dict()
Expand Down Expand Up @@ -886,7 +888,7 @@ def cmd_pull(self):
except NoSuchImageError:
pass
except Exception as exc:
self.client.fail("Error: service image lookup failed - %s" % str(exc))
self.client.fail("Error: service image lookup failed - %s" % to_native(exc))

out_redir_name, err_redir_name = make_redirection_tempfiles()
# pull the image
Expand All @@ -908,7 +910,7 @@ def cmd_pull(self):
if image and image.get('Id'):
new_image_id = image['Id']
except NoSuchImageError as exc:
self.client.fail("Error: service image lookup failed after pull - %s" % str(exc))
self.client.fail("Error: service image lookup failed after pull - %s" % to_native(exc))

if new_image_id != old_image_id:
# if a new image was pulled
Expand Down Expand Up @@ -940,7 +942,7 @@ def cmd_build(self):
except NoSuchImageError:
pass
except Exception as exc:
self.client.fail("Error: service image lookup failed - %s" % str(exc))
self.client.fail("Error: service image lookup failed - %s" % to_native(exc))

out_redir_name, err_redir_name = make_redirection_tempfiles()
# build the image
Expand Down Expand Up @@ -1099,7 +1101,7 @@ def parse_scale(self, service_name):
return int(self.scale[service_name])
except ValueError:
self.client.fail("Error scaling %s - expected int, got %s",
service_name, str(type(self.scale[service_name])))
service_name, to_native(type(self.scale[service_name])))


def main():
Expand Down Expand Up @@ -1142,9 +1144,11 @@ def main():
result = ContainerManager(client).exec_module()
client.module.exit_json(**result)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except RequestException as e:
client.fail('An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc())
client.fail(
'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)),
exception=traceback.format_exc())


if __name__ == '__main__':
Expand Down
6 changes: 4 additions & 2 deletions plugins/modules/docker_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,11 @@ def main():
ConfigManager(client, results)()
client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except RequestException as e:
client.fail('An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc())
client.fail(
'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)),
exception=traceback.format_exc())


if __name__ == '__main__':
Expand Down
18 changes: 10 additions & 8 deletions plugins/modules/docker_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ def __init__(self, client):
try:
setattr(self, param_name, human_to_bytes(client.module.params.get(param_name)))
except ValueError as exc:
self.fail("Failed to convert %s to bytes: %s" % (param_name, exc))
self.fail("Failed to convert %s to bytes: %s" % (param_name, to_native(exc)))

self.publish_all_ports = False
self.published_ports = self._parse_publish_ports()
Expand Down Expand Up @@ -1728,7 +1728,7 @@ def get_default_host_ip(self):
break
except NotFound as nfe:
self.client.fail(
"Cannot inspect the network '{0}' to determine the default IP: {1}".format(net['name'], nfe),
"Cannot inspect the network '{0}' to determine the default IP: {1}".format(net['name'], to_native(nfe)),
exception=traceback.format_exc()
)
return ip
Expand Down Expand Up @@ -1898,7 +1898,7 @@ def _parse_ulimits(self):
try:
results.append(Ulimit(**limits))
except ValueError as exc:
self.fail("Error parsing ulimits value %s - %s" % (limit, exc))
self.fail("Error parsing ulimits value %s - %s" % (limit, to_native(exc)))
return results

def _parse_sysctls(self):
Expand Down Expand Up @@ -1935,7 +1935,7 @@ def _parse_log_config(self):
try:
return LogConfig(**options)
except ValueError as exc:
self.fail('Error parsing logging options - %s' % (exc))
self.fail('Error parsing logging options - %s' % (to_native(exc), ))

def _parse_tmpfs(self):
'''
Expand Down Expand Up @@ -2019,7 +2019,7 @@ def _process_mounts(self):
try:
mount_dict['tmpfs_size'] = human_to_bytes(mount_dict['tmpfs_size'])
except ValueError as exc:
self.fail('Failed to convert tmpfs_size of mount "{0}" to bytes: {1}'.format(target, exc))
self.fail('Failed to convert tmpfs_size of mount "{0}" to bytes: {1}'.format(target, to_native(exc)))
if mount_dict.get('tmpfs_mode') is not None:
try:
mount_dict['tmpfs_mode'] = int(mount_dict['tmpfs_mode'], 8)
Expand Down Expand Up @@ -3164,7 +3164,7 @@ def container_kill(self, container_id):
else:
response = self.client.kill(container_id)
except Exception as exc:
self.fail("Error killing container %s: %s" % (container_id, exc))
self.fail("Error killing container %s: %s" % (container_id, to_native(exc)))
return response

def container_restart(self, container_id):
Expand Down Expand Up @@ -3608,9 +3608,11 @@ def main():
cm = ContainerManager(client)
client.module.exit_json(**sanitize_result(cm.results))
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except RequestException as e:
client.fail('An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc())
client.fail(
'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)),
exception=traceback.format_exc())


if __name__ == '__main__':
Expand Down
12 changes: 7 additions & 5 deletions plugins/modules/docker_container_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
import shlex
import traceback

from ansible.module_utils._text import to_text, to_bytes
from ansible.module_utils._text import to_text, to_bytes, to_native

from ansible_collections.community.docker.plugins.module_utils.common import (
AnsibleDockerClient,
Expand Down Expand Up @@ -244,12 +244,14 @@ def main():
client.fail('Could not find container "{0}"'.format(container))
except APIError as e:
if e.response and e.response.status_code == 409:
client.fail('The container "{0}" has been paused ({1})'.format(container, e))
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
client.fail('The container "{0}" has been paused ({1})'.format(container, to_native(e)))
client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except RequestException as e:
client.fail('An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc())
client.fail(
'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)),
exception=traceback.format_exc())


if __name__ == '__main__':
Expand Down
8 changes: 6 additions & 2 deletions plugins/modules/docker_container_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@

import traceback

from ansible.module_utils._text import to_native

try:
from docker.errors import DockerException
except ImportError:
Expand Down Expand Up @@ -136,9 +138,11 @@ def main():
container=container,
)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except RequestException as e:
client.fail('An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc())
client.fail(
'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)),
exception=traceback.format_exc())


if __name__ == '__main__':
Expand Down
6 changes: 4 additions & 2 deletions plugins/modules/docker_host_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,11 @@ def main():
DockerHostManager(client, results)
client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except RequestException as e:
client.fail('An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc())
client.fail(
'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)),
exception=traceback.format_exc())


if __name__ == '__main__':
Expand Down
24 changes: 13 additions & 11 deletions plugins/modules/docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def absent(self):
# If the image vanished while we were trying to remove it, don't fail
pass
except Exception as exc:
self.fail("Error removing image %s - %s" % (name, str(exc)))
self.fail("Error removing image %s - %s" % (name, to_native(exc)))

self.results['changed'] = True
self.results['actions'].append("Removed image %s" % (name))
Expand Down Expand Up @@ -536,7 +536,7 @@ def archive_image(self, name, tag):
try:
saved_image = self.client.get_image(image_name)
except Exception as exc:
self.fail("Error getting image %s - %s" % (image_name, str(exc)))
self.fail("Error getting image %s - %s" % (image_name, to_native(exc)))

try:
with open(self.archive_path, 'wb') as fd:
Expand All @@ -547,7 +547,7 @@ def archive_image(self, name, tag):
for chunk in saved_image.stream(2048, decode_content=False):
fd.write(chunk)
except Exception as exc:
self.fail("Error writing image archive %s - %s" % (self.archive_path, str(exc)))
self.fail("Error writing image archive %s - %s" % (self.archive_path, to_native(exc)))

if image:
self.results['image'] = image
Expand Down Expand Up @@ -590,11 +590,11 @@ def push_image(self, name, tag=None):
if re.search('unauthorized', str(exc)):
if re.search('authentication required', str(exc)):
self.fail("Error pushing image %s/%s:%s - %s. Try logging into %s first." %
(registry, repo_name, tag, str(exc), registry))
(registry, repo_name, tag, to_native(exc), registry))
else:
self.fail("Error pushing image %s/%s:%s - %s. Does the repository exist?" %
(registry, repo_name, tag, str(exc)))
self.fail("Error pushing image %s: %s" % (repository, str(exc)))
self.fail("Error pushing image %s: %s" % (repository, to_native(exc)))
self.results['image'] = self.client.find_image(name=repository, tag=tag)
if not self.results['image']:
self.results['image'] = dict()
Expand Down Expand Up @@ -634,7 +634,7 @@ def tag_image(self, name, tag, repository, push=False):
if not tag_status:
raise Exception("Tag operation failed.")
except Exception as exc:
self.fail("Error: failed to tag image - %s" % str(exc))
self.fail("Error: failed to tag image - %s" % to_native(exc))
self.results['image'] = self.client.find_image(name=repo, tag=repo_tag)
if image and image['Id'] == self.results['image']['Id']:
self.results['changed'] = False
Expand Down Expand Up @@ -763,10 +763,10 @@ def load_image(self):
)
except EnvironmentError as exc:
if exc.errno == errno.ENOENT:
self.client.fail("Error opening image %s - %s" % (self.load_path, str(exc)))
self.client.fail("Error loading image %s - %s" % (self.name, str(exc)), stdout='\n'.join(load_output))
self.client.fail("Error opening image %s - %s" % (self.load_path, to_native(exc)))
self.client.fail("Error loading image %s - %s" % (self.name, to_native(exc)), stdout='\n'.join(load_output))
except Exception as exc:
self.client.fail("Error loading image %s - %s" % (self.name, str(exc)), stdout='\n'.join(load_output))
self.client.fail("Error loading image %s - %s" % (self.name, to_native(exc)), stdout='\n'.join(load_output))

# Collect loaded images
if has_output:
Expand Down Expand Up @@ -908,9 +908,11 @@ def detect_pull_platform(client):
ImageManager(client, results)
client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except RequestException as e:
client.fail('An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc())
client.fail(
'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)),
exception=traceback.format_exc())


if __name__ == '__main__':
Expand Down
10 changes: 7 additions & 3 deletions plugins/modules/docker_image_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@

import traceback

from ansible.module_utils._text import to_native

try:
from docker import utils
from docker.errors import DockerException, NotFound
Expand Down Expand Up @@ -235,7 +237,7 @@ def get_all_images(self):
except NotFound:
pass
except Exception as exc:
self.fail("Error inspecting image %s - %s" % (image['Id'], str(exc)))
self.fail("Error inspecting image %s - %s" % (image['Id'], to_native(exc)))
results.append(inspection)
return results

Expand All @@ -260,9 +262,11 @@ def main():
ImageManager(client, results)
client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except RequestException as e:
client.fail('An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc())
client.fail(
'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)),
exception=traceback.format_exc())


if __name__ == '__main__':
Expand Down
Loading