|
4 | 4 | # -------------------------------------------------------------------------------------------- |
5 | 5 | import json |
6 | 6 | import re |
| 7 | +import xml.etree.ElementTree as ET |
7 | 8 | from datetime import datetime |
8 | 9 | from enum import Enum |
9 | 10 | import os |
@@ -334,10 +335,32 @@ def handle_asc_exception(ex): |
334 | 335 | try: |
335 | 336 | raise CLIError(ex.inner_exception.error.message) |
336 | 337 | except AttributeError: |
| 338 | + logger.debug(f"CLIError ex: {ex}") |
| 339 | + logger.debug(f"CLIError ex.response.internal_response.text: {ex.response.internal_response.text}") |
337 | 340 | if hasattr(ex, 'response') and ex.response.internal_response.text: |
338 | | - response_dict = json.loads(ex.response.internal_response.text) |
339 | | - raise CLIError(response_dict["error"]["message"]) |
| 341 | + logger.debug("CLIError: Trying to parse the exception message.") |
| 342 | + try: |
| 343 | + response_dict = json.loads(ex.response.internal_response.text) |
| 344 | + logger.debug(f"CLIError response_dict: {response_dict}") |
| 345 | + raise CLIError(response_dict["error"]["message"]) |
| 346 | + except json.JSONDecodeError: |
| 347 | + # Try to extract error from XML format |
| 348 | + try: |
| 349 | + root = ET.fromstring(ex.response.internal_response.text) |
| 350 | + # Look for common error message patterns in XML |
| 351 | + error_msg = root.find('.//Message') |
| 352 | + if error_msg is not None and error_msg.text: |
| 353 | + raise CLIError(error_msg.text) |
| 354 | + # If no Message element, try to find any text content |
| 355 | + error_text = ''.join(root.itertext()).strip() |
| 356 | + if error_text: |
| 357 | + raise CLIError(error_text) |
| 358 | + except ET.ParseError: |
| 359 | + pass |
| 360 | + # If both JSON and XML parsing fail, return the raw text |
| 361 | + raise CLIError(ex.response.internal_response.text) |
340 | 362 | else: |
| 363 | + logger.debug("CLIError: Unable to parse the exception message.") |
341 | 364 | raise CLIError(ex) |
342 | 365 |
|
343 | 366 |
|
|
0 commit comments