[enh] Catch exceptions and return code in api function helper

This commit is contained in:
Jérôme Lebleu 2015-11-13 21:40:28 +01:00
parent f4853f2f4f
commit f3dff47240

View file

@ -87,12 +87,26 @@ def api(namespaces, host='localhost', port=80, routes={},
instead of using the cached one instead of using the cached one
""" """
moulinette = init_interface('api', try:
kwargs={ 'routes': routes, moulinette = init_interface('api',
'use_websocket': use_websocket }, kwargs={
actionsmap={ 'namespaces': namespaces, 'routes': routes,
'use_cache': use_cache }) 'use_websocket': use_websocket
moulinette.run(host, port) },
actionsmap={
'namespaces': namespaces,
'use_cache': use_cache
}
)
moulinette.run(host, port)
except MoulinetteError as e:
import logging
logging.getLogger('moulinette').error(e.strerror)
return e.errno
except KeyboardInterrupt:
import logging
logging.getLogger('moulinette').info(m18n.g('operation_interrupted'))
return 0
def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}): def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}):
"""Command line interface """Command line interface
@ -122,6 +136,6 @@ def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}):
moulinette.run(args, output_as=output_as) moulinette.run(args, output_as=output_as)
except MoulinetteError as e: except MoulinetteError as e:
import logging import logging
logging.getLogger('yunohost').error(e.strerror) logging.getLogger('moulinette').error(e.strerror)
return e.errno return e.errno
return 0 return 0