Merge "Raise specific errors during create configdocs"
This commit is contained in:
commit
1474d7856f
@ -23,6 +23,8 @@ import requests
|
|||||||
from shipyard_client.api_client.client_error import ClientError
|
from shipyard_client.api_client.client_error import ClientError
|
||||||
from shipyard_client.api_client.client_error import UnauthenticatedClientError
|
from shipyard_client.api_client.client_error import UnauthenticatedClientError
|
||||||
from shipyard_client.api_client.client_error import UnauthorizedClientError
|
from shipyard_client.api_client.client_error import UnauthorizedClientError
|
||||||
|
from shipyard_client.api_client.client_error import ShipyardBufferError
|
||||||
|
from shipyard_client.api_client.client_error import InvalidCollectionError
|
||||||
|
|
||||||
|
|
||||||
class BaseClient(metaclass=abc.ABCMeta):
|
class BaseClient(metaclass=abc.ABCMeta):
|
||||||
@ -99,6 +101,10 @@ class BaseClient(metaclass=abc.ABCMeta):
|
|||||||
raise UnauthenticatedClientError()
|
raise UnauthenticatedClientError()
|
||||||
if response.status_code == 403:
|
if response.status_code == 403:
|
||||||
raise UnauthorizedClientError()
|
raise UnauthorizedClientError()
|
||||||
|
if response.status_code == 400:
|
||||||
|
raise InvalidCollectionError(response.text)
|
||||||
|
if response.status_code == 409:
|
||||||
|
raise ShipyardBufferError(response.text)
|
||||||
return response
|
return response
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
self.error(str(e))
|
self.error(str(e))
|
||||||
|
@ -23,3 +23,11 @@ class UnauthorizedClientError(ClientError):
|
|||||||
|
|
||||||
class UnauthenticatedClientError(ClientError):
|
class UnauthenticatedClientError(ClientError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ShipyardBufferError(ClientError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidCollectionError(ClientError):
|
||||||
|
pass
|
||||||
|
@ -19,6 +19,8 @@ import logging
|
|||||||
from shipyard_client.api_client.client_error import ClientError
|
from shipyard_client.api_client.client_error import ClientError
|
||||||
from shipyard_client.api_client.client_error import UnauthenticatedClientError
|
from shipyard_client.api_client.client_error import UnauthenticatedClientError
|
||||||
from shipyard_client.api_client.client_error import UnauthorizedClientError
|
from shipyard_client.api_client.client_error import UnauthorizedClientError
|
||||||
|
from shipyard_client.api_client.client_error import ShipyardBufferError
|
||||||
|
from shipyard_client.api_client.client_error import InvalidCollectionError
|
||||||
from shipyard_client.api_client.shipyard_api_client import ShipyardClient
|
from shipyard_client.api_client.shipyard_api_client import ShipyardClient
|
||||||
from shipyard_client.api_client.shipyardclient_context import \
|
from shipyard_client.api_client.shipyardclient_context import \
|
||||||
ShipyardClientContext
|
ShipyardClientContext
|
||||||
@ -134,6 +136,10 @@ class CliAction(AbstractCliAction):
|
|||||||
"Check credential values")
|
"Check credential values")
|
||||||
except UnauthorizedClientError:
|
except UnauthorizedClientError:
|
||||||
self.resp_txt = "Error: Unauthorized to perform this action."
|
self.resp_txt = "Error: Unauthorized to perform this action."
|
||||||
|
except ShipyardBufferError as ex:
|
||||||
|
self.resp_txt = format_utils.cli_format_exception_handler(ex)
|
||||||
|
except InvalidCollectionError as ex:
|
||||||
|
self.resp_txt = format_utils.cli_format_exception_handler(ex)
|
||||||
except ClientError as ex:
|
except ClientError as ex:
|
||||||
self.resp_txt = "Error: Client Error: {}".format(str(ex))
|
self.resp_txt = "Error: Client Error: {}".format(str(ex))
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
|
@ -47,11 +47,7 @@ def cli_format_status_handler(response, is_error=False):
|
|||||||
standard error format
|
standard error format
|
||||||
:is_error: toggles the use of status or error verbiage
|
:is_error: toggles the use of status or error verbiage
|
||||||
:returns: a generically formatted error response formulated from the
|
:returns: a generically formatted error response formulated from the
|
||||||
client_repsonse. The response will be in the format:
|
client_repsonse.
|
||||||
|
|
||||||
[Error|Status]: {{message}}
|
|
||||||
Reason: {{Reason}}
|
|
||||||
Additional: {{details message list messages}}
|
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
formatted = "Error: {}\nReason: {}" if is_error \
|
formatted = "Error: {}\nReason: {}" if is_error \
|
||||||
@ -59,13 +55,42 @@ def cli_format_status_handler(response, is_error=False):
|
|||||||
try:
|
try:
|
||||||
if response.text:
|
if response.text:
|
||||||
resp_j = response.json()
|
resp_j = response.json()
|
||||||
resp = formatted.format(resp_j.get('message', 'Not specified'),
|
return cli_format_response(formatted, resp_j)
|
||||||
resp_j.get('reason', 'Not specified'))
|
else:
|
||||||
|
return ''
|
||||||
|
except ValueError:
|
||||||
|
return "Error: Unable to decode response. Value: {}".format(
|
||||||
|
response.text)
|
||||||
|
|
||||||
|
def cli_format_exception_handler(exc_msg):
|
||||||
|
""" Formatter for custom error raised by Shipyard """
|
||||||
|
try:
|
||||||
|
formatted = "Error: {}\nReason: {}"
|
||||||
|
# Convert exception message to dict from string
|
||||||
|
exc_dict = json.loads(str(exc_msg))
|
||||||
|
return cli_format_response(formatted, exc_dict)
|
||||||
|
except Exception:
|
||||||
|
return "Error: Unable to decode response. Value: {}".format(
|
||||||
|
exc_msg)
|
||||||
|
|
||||||
|
def cli_format_response(formatted, response):
|
||||||
|
""" Handler for Shipyard status and error responses
|
||||||
|
|
||||||
|
:param formatted: structure to be used for response
|
||||||
|
:param response: client response to be formatted
|
||||||
|
|
||||||
|
The response will be in the format:
|
||||||
|
[Error|Status]: {{message}}
|
||||||
|
Reason: {{Reason}}
|
||||||
|
Additional: {{details message list messages}}
|
||||||
|
"""
|
||||||
|
resp = formatted.format(response.get('message', 'Not specified'),
|
||||||
|
response.get('reason', 'Not specified'))
|
||||||
# lvl_counts must have a matching number of values as the
|
# lvl_counts must have a matching number of values as the
|
||||||
# _LEVEL_KEYS below + 1 for sentinel.
|
# _LEVEL_KEYS below + 1 for sentinel.
|
||||||
lvl_counts = [0, 0, 0, 0]
|
lvl_counts = [0, 0, 0, 0]
|
||||||
if resp_j.get('details'):
|
if response.get('details'):
|
||||||
mlist = resp_j['details'].get('messageList', [])
|
mlist = response['details'].get('messageList', [])
|
||||||
# Decorate messages with level number and sortkey
|
# Decorate messages with level number and sortkey
|
||||||
for message in mlist:
|
for message in mlist:
|
||||||
message['lnum'], message['sortkey'] = _lvl_key(
|
message['lnum'], message['sortkey'] = _lvl_key(
|
||||||
@ -91,11 +116,6 @@ def cli_format_status_handler(response, is_error=False):
|
|||||||
" Infos: {},"
|
" Infos: {},"
|
||||||
" Other: {} ####".format(*lvl_counts))
|
" Other: {} ####".format(*lvl_counts))
|
||||||
return resp
|
return resp
|
||||||
else:
|
|
||||||
return ''
|
|
||||||
except ValueError:
|
|
||||||
return "Error: Unable to decode response. Value: {}".format(
|
|
||||||
response.text)
|
|
||||||
|
|
||||||
|
|
||||||
# Map of levels by severity. Extra values are included in this map but valid
|
# Map of levels by severity. Extra values are included in this map but valid
|
||||||
|
Loading…
Reference in New Issue
Block a user