Merge branch 'develop' into dist/risotto/risotto-2.8.0/develop

This commit is contained in:
Emmanuel Garette 2021-04-24 12:57:14 +02:00
commit fbf4fc9643
3 changed files with 21 additions and 9 deletions

View File

@ -4,7 +4,7 @@
from sys import exit, argv from sys import exit, argv
from json import dumps from json import dumps
from traceback import print_exc from traceback import print_exc
from cucchiaiata import Parser, config, Configuration from cucchiaiata import Parser, config, Configuration, JsonError
from cucchiaiata.i18n import _ from cucchiaiata.i18n import _
@ -15,9 +15,15 @@ def main():
else: else:
parser = Parser() parser = Parser()
print(dumps(parser.get(), print(dumps(parser.get(),
indent=config.indent)) indent=config.indent),
)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
except JsonError as err:
print(dumps(err.message,
indent=config.indent),
)
exit(1)
except Exception as err: except Exception as err:
if config.debug: if config.debug:
print_exc() print_exc()

View File

@ -1,6 +1,7 @@
from .parser import Parser from .parser import Parser
from .configuration import Configuration from .configuration import Configuration
from .config import config from .config import config
from .common import JsonError
__all__ = ('Parser', 'config') __all__ = ('Parser', 'config', 'Configuration', 'JsonError')
__version__ = "0.0.1" __version__ = "0.0.1"

View File

@ -13,6 +13,10 @@ if config.allow_insecure_https:
warnings.simplefilter('ignore', InsecureRequestWarning) warnings.simplefilter('ignore', InsecureRequestWarning)
class JsonError(Exception):
pass
class Common: class Common:
def __init__(self): def __init__(self):
self.cucchiaiata_config = config self.cucchiaiata_config = config
@ -97,11 +101,12 @@ def send_data(uri: str,
ret = post(final_url, ret = post(final_url,
data=dumps(payload), data=dumps(payload),
verify=config.allow_insecure_https) verify=config.allow_insecure_https)
if ret.status_code != 200: try:
response = ret.json()
except:
raise Exception(ret.text) raise Exception(ret.text)
response = ret.json() if response['type'] == 'error':
if 'error' in response: err = JsonError()
if 'reason' in response['error']['kwargs']: err.message = response['response']
raise Exception("{}".format(response['error']['kwargs']['reason'])) raise err
raise Exception('erreur inconnue')
return response['response'] return response['response']