Skip to content

Commit 206c286

Browse files
authored
Merge pull request #3 from cloudera-labs/devel
Fix Error messages not always containing standard fields and strings.
2 parents 4e01112 + 3ba4f5b commit 206c286

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

README.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ For General usage, installed from cmdline ::
1616

1717
pip install cdpy
1818

19+
To install the development branch instead of main ::
20+
21+
git+git://github.com/cloudera-labs/cdpy@devel#egg=cdpy
22+
1923
Usage
2024
=====
2125
Note that this readme covers usage of this wrapper library only, for details of CDPCLI and underlying commands please see the CDPCLI documentation.
@@ -100,8 +104,8 @@ This example also demonstrates bypassing the Squelch error handler to implement
100104
)
101105
if isinstance(resp, CdpError):
102106
if resp.error_code == 'INVALID_ARGUMENT':
103-
if 'constraintViolations' not in resp.violations:
104-
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(resp.violations))
107+
if 'constraintViolations' not in str(resp.violations):
108+
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(str(resp.violations)))
105109
self.sdk.throw_warning(resp)
106110
self.sdk.throw_error(resp)
107111
return resp

src/cdpy/common.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,14 @@ def __init__(self, base_error, *args):
5555
try:
5656
_violations = json.loads(html.unescape(self.base_error.response['error']['message']))
5757
except JSONDecodeError:
58-
_violations = self.base_error.response['error']['message']
59-
self.error_code = self.base_error.response['error']['code']
58+
try:
59+
_violations = self.base_error.response['error']['message']
60+
except KeyError:
61+
_violations = self.base_error.args[0]
62+
try:
63+
self.error_code = self.base_error.response['error']['code']
64+
except KeyError:
65+
self.error_code = ''
6066
self.violations = _violations
6167
self.status_code = _payload.group(1)
6268
self.rc = 1

src/cdpy/environments.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def describe_environment(self, name):
6161
# Describe will fail in certain fault scenarios early in Environment creation
6262
# We helpfully provide the summary listing as a backup set of information
6363
# If the environment truly does not exist, then this will give the same response
64-
self.sdk.throw_warning(CdpWarning(resp.violations))
64+
self.sdk.throw_warning(CdpWarning(str(resp.violations)))
6565
return self.summarize_environment(name)
6666
self.sdk.throw_error(resp)
6767
return resp
@@ -105,9 +105,9 @@ def create_aws_environment(self, **kwargs):
105105
)
106106
if isinstance(resp, CdpError):
107107
if resp.error_code == 'INVALID_ARGUMENT':
108-
if 'constraintViolations' not in resp.violations:
109-
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(resp.violations))
110-
self.sdk.throw_warning(CdpWarning(resp.violations))
108+
if 'constraintViolations' not in str(resp.violations):
109+
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(str(resp.violations)))
110+
self.sdk.throw_warning(CdpWarning(str(resp.violations)))
111111
self.sdk.throw_error(resp)
112112
return resp
113113

@@ -119,9 +119,9 @@ def create_azure_environment(self, **kwargs):
119119
)
120120
if isinstance(resp, CdpError):
121121
if resp.error_code == 'INVALID_ARGUMENT':
122-
if 'constraintViolations' not in resp.violations:
123-
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(resp.violations))
124-
self.sdk.throw_warning(CdpWarning(resp.violations))
122+
if 'constraintViolations' not in str(resp.violations):
123+
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(str(resp.violations)))
124+
self.sdk.throw_warning(CdpWarning(str(resp.violations)))
125125
self.sdk.throw_error(resp)
126126
return resp
127127

@@ -133,9 +133,9 @@ def create_gcp_environment(self, **kwargs):
133133
)
134134
if isinstance(resp, CdpError):
135135
if resp.error_code == 'INVALID_ARGUMENT':
136-
if 'constraintViolations' not in resp.violations:
137-
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(resp.violations))
138-
self.sdk.throw_warning(CdpWarning(resp.violations))
136+
if 'constraintViolations' not in str(resp.violations):
137+
resp.update(message="Received violation warning:\n%s" % self.sdk.dumps(str(resp.violations)))
138+
self.sdk.throw_warning(CdpWarning(str(resp.violations)))
139139
self.sdk.throw_error(resp)
140140
return resp
141141

@@ -205,7 +205,7 @@ def sync_users(self, environments=None):
205205
)
206206
if isinstance(resp, CdpError):
207207
if resp.error_code == 'CONFLICT':
208-
operation_match = self.sdk.regex_search(self.sdk.OPERATION_REGEX, resp.violations)
208+
operation_match = self.sdk.regex_search(self.sdk.OPERATION_REGEX, str(resp.violations))
209209
if operation_match is not None:
210210
existing_op_id = operation_match.group(1)
211211
if not self.sdk.strict_errors:
@@ -264,11 +264,11 @@ def create_aws_credential(self, name, role, description, retries=3, delay=2):
264264
consistency_violations = [
265265
'Unable to verify credential', 'sts:AssumeRole', 'You are not authorized'
266266
]
267-
if any(x in resp.violations for x in consistency_violations):
267+
if any(x in str(resp.violations) for x in consistency_violations):
268268
retries = retries - 1
269269
self.sdk.throw_warning(
270270
CdpWarning('Got likely AWS IAM eventual consistency error [%s], %d retries left...'
271-
% (resp.violations, retries))
271+
% (str(resp.violations), retries))
272272
)
273273
self.sdk.sleep(delay)
274274
return self.create_aws_credential(name, role, description, retries, delay)

0 commit comments

Comments
 (0)