Coverage for lib/ansible/errors/__init__.py : 57%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com> # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
YAML_COMMON_DICT_ERROR, YAML_COMMON_LEADING_TAB_ERROR, YAML_COMMON_PARTIALLY_QUOTED_LINE_ERROR, YAML_COMMON_UNBALANCED_QUOTES_ERROR, YAML_COMMON_UNQUOTED_COLON_ERROR, YAML_COMMON_UNQUOTED_VARIABLE_ERROR, YAML_POSITION_DETAILS, )
''' This is the base class for all errors raised from Ansible code, and can be instantiated with two optional parameters beyond the error message to control whether detailed information is displayed when the error occurred while parsing a data file of some kind.
Usage:
raise AnsibleError('some message here', obj=obj, show_content=True)
Where "obj" is some subclass of ansible.parsing.yaml.objects.AnsibleBaseYAMLObject, which should be returned by the DataLoader() class. '''
# we import this here to prevent an import loop problem, # since the objects code also imports ansible.errors
else: self.message = '%s' % to_native(message) else:
return self.message
''' Returns the line in the file which corresponds to the reported error location, as well as the line preceding it (if the error did not occur on the first line), to provide context to the error. '''
''' Given an object reporting the location of the exception in a file, return detailed information regarding it including:
* the line which caused the error as well as the one preceding it * causes and suggested remedies for common syntax errors
If this error was created with show_content=False, the reporting of content is suppressed, as the file contents may be sensitive (ie. vault data). '''
# header_line = ("=" * 73)
# TODO: There may be cases where there is a valid tab in a line that has other errors. error_message += YAML_COMMON_LEADING_TAB_ERROR # common error/remediation checking here: # check for unquoted vars starting lines error_message += YAML_COMMON_UNQUOTED_VARIABLE_ERROR # check for common dictionary mistakes error_message += YAML_COMMON_DICT_ERROR # check for common unquoted colon mistakes len(target_line) > 1 and len(target_line) > col_number and target_line[col_number] == ":" and target_line.count(':') > 1): error_message += YAML_COMMON_UNQUOTED_COLON_ERROR # otherwise, check for some common quoting mistakes else: middle = parts[1].strip() match = False unbalanced = False
if middle.startswith("'") and not middle.endswith("'"): match = True elif middle.startswith('"') and not middle.endswith('"'): match = True
if (len(middle) > 0 and middle[0] in ['"', "'"] and middle[-1] in ['"', "'"] and target_line.count("'") > 2 or target_line.count('"') > 2): unbalanced = True
if match: error_message += YAML_COMMON_PARTIALLY_QUOTED_LINE_ERROR if unbalanced: error_message += YAML_COMMON_UNBALANCED_QUOTES_ERROR
except (IOError, TypeError): error_message += '\n(could not open file to display line)' except IndexError: error_message += '\n(specified line no longer in file, maybe it changed?)'
'''Invalid assertion'''
''' bad or incomplete options passed '''
''' something was detected early that is wrong about a playbook or data file '''
''' internal safeguards tripped, something happened in the code that should never happen '''
''' ansible had a problem while running a playbook '''
''' a module failed somehow '''
''' the transport / connection_plugin had a fatal error '''
''' a templating failure '''
''' a lookup failure '''
''' a callback failure '''
''' a templating failure '''
''' a file missing failure '''
self.file_name = file_name self.paths = paths
if self.file_name: if message: message += "\n" message += "Could not find or access '%s'" % to_text(self.file_name)
if self.paths and isinstance(self.paths, Sequence): searched = to_text('\n\t'.join(self.paths)) if message: message += "\n" message += "Searched in:\n\t%s" % searched
super(AnsibleFileNotFound, self).__init__(message=message, obj=obj, show_content=show_content, suppress_extended_error=suppress_extended_error, orig_exc=orig_exc)
# These Exceptions are temporary, using them as flow control until we can get a better solution. # DO NOT USE as they will probably be removed soon. # We will port the action modules in our tree to use a context manager instead. ''' Base Exception for Action plugin flow control '''
super(AnsibleAction, self).__init__(message=message, obj=obj, show_content=show_content, suppress_extended_error=suppress_extended_error, orig_exc=orig_exc) if result is None: self.result = {} else: self.result = result
''' an action runtime skip'''
super(AnsibleActionSkip, self).__init__(message=message, obj=obj, show_content=show_content, suppress_extended_error=suppress_extended_error, orig_exc=orig_exc, result=result) self.result.update({'skipped': True, 'msg': message})
''' an action runtime failure''' super(AnsibleActionFail, self).__init__(message=message, obj=obj, show_content=show_content, suppress_extended_error=suppress_extended_error, orig_exc=orig_exc, result=result) self.result.update({'failed': True, 'msg': message})
''' an action runtime early exit''' |