Skip to content

Commit

Permalink
Decoding openshift errors for non-200 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mmilata committed Aug 5, 2015
1 parent 932a361 commit 784da9d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion osbs/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from osbs.api import OSBS
from osbs.conf import Configuration
from osbs.constants import DEFAULT_CONFIGURATION_FILE, DEFAULT_CONFIGURATION_SECTION
from osbs.exceptions import OsbsNetworkException, OsbsException, OsbsAuthException
from osbs.exceptions import OsbsNetworkException, OsbsException, OsbsAuthException, OsbsResponseException


logger = logging.getLogger('osbs')
Expand Down Expand Up @@ -392,6 +392,16 @@ def main():
logger.error("Authentication failure: %s",
ex.message)
return -1
except OsbsResponseException as ex:
if is_verbose:
raise
else:
if isinstance(ex.json, dict) and 'message' in ex.json:
msg = ex.json['message']
else:
msg = ex.message
logger.error("Server returned error %s: %s", ex.status_code, msg)
return -1
except Exception as ex: # pylint: disable=broad-except
if is_verbose:
raise
Expand Down
9 changes: 9 additions & 0 deletions osbs/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
from __future__ import print_function, absolute_import, unicode_literals

import json
from traceback import format_tb


Expand Down Expand Up @@ -45,6 +46,14 @@ class OsbsResponseException(OsbsException):
def __init__(self, message, status_code, *args, **kwargs):
super(OsbsResponseException, self).__init__(message, *args, **kwargs)
self.status_code = status_code
self.json = None

# try decoding openshift Status object
# https://docs.openshift.org/latest/rest_api/openshift_v1.html#v1-status
try:
self.json = json.loads(message)
except ValueError:
pass


class OsbsNetworkException(OsbsException):
Expand Down

0 comments on commit 784da9d

Please sign in to comment.