Used when a module runs a command which returns an error and wants to show the user the output gracefully instead of dying
| 111 | |
| 112 | |
| 113 | class CommandExecutionError(SaltException): |
| 114 | """ |
| 115 | Used when a module runs a command which returns an error and wants |
| 116 | to show the user the output gracefully instead of dying |
| 117 | """ |
| 118 | |
| 119 | def __init__(self, message="", info=None): |
| 120 | # Avoid circular import |
| 121 | import salt.utils.stringutils |
| 122 | |
| 123 | try: |
| 124 | exc_str_prefix = salt.utils.stringutils.to_unicode(message) |
| 125 | except TypeError: |
| 126 | # Exception class instance passed. The SaltException __init__ will |
| 127 | # gracefully handle non-string types passed to it, but since this |
| 128 | # class needs to do some extra stuff with the exception "message" |
| 129 | # before handing it off to the parent class' __init__, we'll need |
| 130 | # to extract the message from the exception instance here |
| 131 | try: |
| 132 | exc_str_prefix = str(message) |
| 133 | except UnicodeDecodeError: |
| 134 | exc_str_prefix = salt.utils.stringutils.to_unicode(str(message)) |
| 135 | self.error = exc_str_prefix |
| 136 | self.info = info |
| 137 | if self.info: |
| 138 | if exc_str_prefix: |
| 139 | if exc_str_prefix[-1] not in ".?!": |
| 140 | exc_str_prefix += "." |
| 141 | exc_str_prefix += " " |
| 142 | |
| 143 | exc_str_prefix += "Additional info follows:\n\n" |
| 144 | # NOTE: exc_str will be passed to the parent class' constructor and |
| 145 | # become self.strerror. |
| 146 | exc_str = exc_str_prefix + _nested_output(self.info) |
| 147 | |
| 148 | # For states, if self.info is a dict also provide an attribute |
| 149 | # containing a nested output of the info dict without the changes |
| 150 | # (since they will be in the 'changes' key of the state return and |
| 151 | # this information would be redundant). |
| 152 | if isinstance(self.info, dict): |
| 153 | info_without_changes = copy.deepcopy(self.info) |
| 154 | info_without_changes.pop("changes", None) |
| 155 | if info_without_changes: |
| 156 | self.strerror_without_changes = exc_str_prefix + _nested_output( |
| 157 | info_without_changes |
| 158 | ) |
| 159 | else: |
| 160 | # 'changes' was the only key in the info dictionary. We no |
| 161 | # longer have any additional info to display. Use the |
| 162 | # original error message. |
| 163 | self.strerror_without_changes = self.error |
| 164 | else: |
| 165 | self.strerror_without_changes = exc_str |
| 166 | else: |
| 167 | self.strerror_without_changes = exc_str = self.error |
| 168 | |
| 169 | # We call the parent __init__ last instead of first because we need the |
| 170 | # logic above to derive the message string to use for the exception |
no outgoing calls