summaryrefslogtreecommitdiff
path: root/lib/ansible/errors
diff options
context:
space:
mode:
authorMarius Gedminas <marius@gedmin.as>2015-10-15 09:38:14 +0300
committerMarius Gedminas <marius@gedmin.as>2015-10-15 09:38:14 +0300
commitca826508d992f99ab6cba75d03a34b804448dfef (patch)
treeb1878dacfdc7f79110de6a21c0779a10e9c6ae4a /lib/ansible/errors
parent5617f6aad459e9466326316b0207a094d692e409 (diff)
downloadansible-ca826508d992f99ab6cba75d03a34b804448dfef.tar.gz
Python 3: fix AnsibleError formatting
If you convert the error string to bytes and embed it inside another error string, you get Prefix: b'Embedded\nerror\nstring' which is not what we want. But we also don't want Unicode in error messages causing unexpected UnicodeEncodeErrors when on Python 2. So let's convert the error message into the native string type (bytes on Python 2, unicode on Python 3).
Diffstat (limited to 'lib/ansible/errors')
-rw-r--r--lib/ansible/errors/__init__.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/lib/ansible/errors/__init__.py b/lib/ansible/errors/__init__.py
index f46c4f34c9..c88f2bb864 100644
--- a/lib/ansible/errors/__init__.py
+++ b/lib/ansible/errors/__init__.py
@@ -19,11 +19,18 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
-import os
-
from ansible.errors.yaml_strings import *
from ansible.utils.unicode import to_unicode, to_bytes
+
+if str is bytes:
+ # Python 2
+ to_str = to_bytes
+else:
+ # Python 3
+ to_str = to_unicode
+
+
class AnsibleError(Exception):
'''
This is the base class for all errors raised from Ansible code,
@@ -49,7 +56,7 @@ class AnsibleError(Exception):
if obj and isinstance(obj, AnsibleBaseYAMLObject):
extended_error = self._get_extended_error()
if extended_error:
- self.message = 'ERROR! %s\n\n%s' % (message, to_bytes(extended_error))
+ self.message = 'ERROR! %s\n\n%s' % (message, to_str(extended_error))
else:
self.message = 'ERROR! %s' % message