Skip to content

Commit bfa4e20

Browse files
authored
Error handling for client/server call mismatches (#25)
* Add error handling for when the user requests a call from cdpy that is not available in the installed CDPCLI * Add error handling for when CDPCLI makes a call that is not available in the remote CDP Control Plane * Move Custom Error handling functionality into the CdpError class and out of call() * Directly Handle parsing AttributeError and ValidationError into common format * Move Handling REMOTE_NOT_IMPLMENTED in CdpError Signed-off-by: Daniel Chaffelson <chaffelson@gmail.com>
1 parent dafc88d commit bfa4e20

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/cdpy/common.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from cdpcli.client import ClientCreator, Context
2121
from cdpcli.credentials import Credentials
2222
from cdpcli.endpoint import EndpointCreator, EndpointResolver
23-
from cdpcli.exceptions import ClientError, ParamValidationError
23+
from cdpcli.exceptions import ClientError, ParamValidationError, ValidationError
2424
from cdpcli.loader import Loader
2525
from cdpcli.parser import ResponseParserFactory
2626
from cdpcli.retryhandler import create_retry_handler
@@ -47,6 +47,20 @@ def __init__(self, base_error, *args):
4747
self.operation = None
4848
self.request_id = None
4949

50+
if isinstance(self.base_error, AttributeError):
51+
self.rc = 1
52+
self.status_code = '404'
53+
self.error_code = "LOCAL_NOT_IMPLEMENTED"
54+
self.violations = self.message = str(self.base_error) + ". The installed CDPCLI does not support this call."
55+
56+
if isinstance(self.base_error, ValidationError):
57+
self.message = self.violations = str(self.base_error) + ". The installed CDPCLI does not support this call."
58+
self.rc = 1
59+
self.status_code = '404'
60+
self.operation = self.base_error.kwargs['param']
61+
self.service = self.base_error.kwargs['value']
62+
self.error_code = 'LOCAL_NOT_IMPLEMENTED'
63+
5064
if isinstance(self.base_error, ClientError):
5165
_CLIENT_ERROR_PATTERN = re.compile(
5266
r"Status Code: (.*?); Error Code: (.*?); Service: "
@@ -82,6 +96,16 @@ def __init__(self, base_error, *args):
8296
self.message = "Parameter validation error"
8397
self.error_code = 'PARAMETER_VALIDATION_ERROR'
8498

99+
# Handle instance where client calls function not found in remote CDP Control Plane
100+
if self.status_code == '404' \
101+
and self.error_code == 'UNKNOWN_ERROR' \
102+
and 'HTTP ERROR 404 Not Found' in self.message:
103+
self.error_code = "REMOTE_NOT_IMPLEMENTED"
104+
self.violations = "Function {0} in Remote Service {1} was not found. " \
105+
"Your connected CDP Control Plane may not support this call. " \
106+
"Rerun this call with strict_errors enabled to get full traceback." \
107+
.format(self.operation, self.service)
108+
85109
super().__init__(base_error, *args)
86110

87111
def update(self, *args, **kwargs):
@@ -515,12 +539,13 @@ def call(self, svc: str, func: str, ret_field: str = None, squelch: ['Squelch']
515539
parsed_err.update(sdk_out=log, sdk_out_lines=log.splitlines())
516540
if self.strict_errors is True:
517541
self.throw_error(parsed_err)
518-
if isinstance(err, ClientError) and squelch is not None:
519-
for item in squelch:
520-
if item.value in str(parsed_err.__getattribute__(item.field)):
521-
warning = item.warning if item.warning is not None else str(parsed_err.violations)
522-
self.throw_warning(CdpWarning(warning))
523-
return item.default
542+
if isinstance(err, ClientError):
543+
if squelch is not None:
544+
for item in squelch:
545+
if item.value in str(parsed_err.__getattribute__(item.field)):
546+
warning = item.warning if item.warning is not None else str(parsed_err.violations)
547+
self.throw_warning(CdpWarning(warning))
548+
return item.default
524549
if ret_error is True:
525550
return parsed_err
526551
self.throw_error(parsed_err)

0 commit comments

Comments
 (0)