|
10 | 10 | from azure.cli.core.profiles import ResourceType |
11 | 11 |
|
12 | 12 | from knack.log import get_logger |
| 13 | +from knack.util import CLIError |
13 | 14 | from knack.validators import DefaultStr, DefaultInt # pylint: disable=unused-import |
14 | 15 |
|
15 | 16 | logger = get_logger(__name__) |
@@ -84,12 +85,26 @@ def get_default_location_from_resource_group(cmd, namespace): |
84 | 85 | def validate_file_or_dict(string): |
85 | 86 | import os |
86 | 87 | string = os.path.expanduser(string) |
87 | | - if os.path.exists(string): |
88 | | - from azure.cli.core.util import get_file_json |
89 | | - return get_file_json(string) |
90 | | - |
91 | | - from azure.cli.core.util import shell_safe_json_parse |
92 | | - return shell_safe_json_parse(string) |
| 88 | + try: |
| 89 | + if os.path.exists(string): |
| 90 | + from azure.cli.core.util import get_file_json |
| 91 | + # Failure point 1: 'string' is a valid file path, but the file contains invalid JSON string |
| 92 | + # ex has no recommendation |
| 93 | + return get_file_json(string) |
| 94 | + |
| 95 | + from azure.cli.core.util import shell_safe_json_parse |
| 96 | + # Failure point 2: |
| 97 | + # - 'string' is a non-existing file path |
| 98 | + # - 'string' is an invalid JSON string |
| 99 | + # ex has recommendations for shell interpretation |
| 100 | + return shell_safe_json_parse(string) |
| 101 | + except CLIError as ex: |
| 102 | + from azure.cli.core.azclierror import InvalidArgumentValueError |
| 103 | + new_ex = InvalidArgumentValueError(ex, recommendation="Please provide a valid JSON file path or JSON string.") |
| 104 | + # Preserve the recommendation |
| 105 | + if hasattr(ex, "recommendations"): |
| 106 | + new_ex.set_recommendation(ex.recommendations) |
| 107 | + raise new_ex from ex |
93 | 108 |
|
94 | 109 |
|
95 | 110 | def validate_parameter_set(namespace, required, forbidden, dest_to_options=None, description=None): |
|
0 commit comments