diff --git a/src/risotto/controller.py b/src/risotto/controller.py index 1ac8223..1a829b8 100644 --- a/src/risotto/controller.py +++ b/src/risotto/controller.py @@ -20,10 +20,10 @@ class Controller: version, module, message = uri.split('.', 2) uri = module + '.' + message if module not in self.risotto_modules: - return await remote.call_or_publish(module, - version, - message, - kwargs) + return await remote.remove_call(module, + version, + message, + kwargs) return await dispatcher.call(version, uri, risotto_context, @@ -37,14 +37,15 @@ class Controller: version, module, submessage = uri.split('.', 2) version, message = uri.split('.', 1) if module not in self.risotto_modules: - await remote.call_or_publish(module, - version, - submessage, - kwargs) - await dispatcher.publish(version, - message, - risotto_context, - **kwargs) + await remote.remove_call(module, + version, + submessage, + kwargs) + else: + await dispatcher.publish(version, + message, + risotto_context, + **kwargs) async def on_join(self, risotto_context): diff --git a/src/risotto/dispatcher.py b/src/risotto/dispatcher.py index a9d4ae9..301d4ea 100644 --- a/src/risotto/dispatcher.py +++ b/src/risotto/dispatcher.py @@ -134,7 +134,10 @@ class PublishDispatcher: version, message, 'event') - function_objs = self.messages[version][message].get('functions', []) + try: + function_objs = self.messages[version][message].get('functions', []) + except KeyError: + raise ValueError(_(f'cannot find message {version}.{message}')) # do not start a new database connection if hasattr(old_risotto_context, 'connection'): risotto_context.connection = old_risotto_context.connection diff --git a/src/risotto/logger.py b/src/risotto/logger.py index 12f9f36..ae9f89e 100644 --- a/src/risotto/logger.py +++ b/src/risotto/logger.py @@ -1,5 +1,8 @@ from typing import Dict, Any from json import dumps +from asyncpg.exceptions import UndefinedTableError + + from .context import Context from .utils import _ from .config import get_config @@ -23,7 +26,10 @@ class Logger: args.append(dumps(data)) sql = insert + ') ' + values + ')' - await risotto_context.connection.fetch(sql, *args) + try: + await risotto_context.connection.fetch(sql, *args) + except UndefinedTableError as err: + raise Exception(_(f'cannot access to database ({err}), was the database really created?')) def _get_message_paths(self, risotto_context: Context): diff --git a/src/risotto/remote.py b/src/risotto/remote.py index a95d6ca..777b540 100644 --- a/src/risotto/remote.py +++ b/src/risotto/remote.py @@ -5,18 +5,19 @@ from tiramisu_api import Config from .config import get_config +from .utils import _ # # -# ALLOW_INSECURE_HTTPS = get_config()['submodule']['allow_insecure_https'] +# ALLOW_INSECURE_HTTPS = get_config()['module']['allow_insecure_https'] class Remote: submodules = {} async def _get_config(self, - submodule: str, + module: str, url: str) -> None: - if submodule not in self.submodules: + if module not in self.submodules: session = ClientSession() async with session.get(url) as resp: if resp.status != 200: @@ -27,22 +28,25 @@ class Remote: err = await resp.text() raise Exception(err) json = await resp.json() - self.submodules[submodule] = json - return Config(self.submodules[submodule]) + self.submodules[module] = json + return Config(self.submodules[module]) async def remove_call(self, - submodule: str, + module: str, version: str, - message: str, + submessage: str, payload) -> dict: - domain_name = get_config()['submodule'][submodule] + try: + domain_name = get_config()['module'][module] + except KeyError: + raise ValueError(_(f'cannot find information of remote module "{module}" to access to "{version}.{module}.{submessage}"')) remote_url = f'http://{domain_name}:8080/api/{version}' - message_url = f'{remote_url}/{message}' + message_url = f'{remote_url}/{submessage}' - config = await self._get_config(submodule, + config = await self._get_config(module, remote_url) for key, value in payload.items(): - path = message + '.' + key + path = submessage + '.' + key config.option(path).value.set(value) session = ClientSession() async with session.post(message_url, data=dumps(payload)) as resp: