Skip to content

Commit

Permalink
Python 3: fix AnsibleError formatting
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
mgedmin committed Oct 15, 2015
1 parent 5617f6a commit ca82650
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/ansible/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -49,7 +56,7 @@ def __init__(self, message, obj=None, show_content=True):
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

Expand Down

0 comments on commit ca82650

Please sign in to comment.