Skip to content

Commit 977111b

Browse files
Human-readable exceptions (#40)
1 parent 049f0e9 commit 977111b

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ authors = [
99
]
1010
readme = "README.md"
1111
repository = "https://github.com/dipdup-net/dipdup-py"
12-
homepage = "https://pytezos.org"
12+
homepage = "https://dipdup.net/"
1313
keywords = ['tezos', 'blockchain', 'sdk', 'michelson', 'indexers', 'tzkt', 'cryptocurrencies', 'smart-contracts']
1414
classifiers = [
1515
"Programming Language :: Python :: 3",

src/dipdup/codegen.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
TzktDatasourceConfig,
2222
)
2323
from dipdup.datasources.tzkt.datasource import TzktDatasource
24+
from dipdup.exceptions import ConfigurationError
2425
from dipdup.utils import camel_to_snake, snake_to_camel
2526

2627
_logger = logging.getLogger(__name__)
@@ -107,9 +108,17 @@ async def fetch_schemas(config: DipDupConfig):
107108
with suppress(FileExistsError):
108109
mkdir(parameter_schemas_path)
109110

110-
entrypoint_schema = next(
111-
ep['parameterSchema'] for ep in contract_schemas['entrypoints'] if ep['name'] == operation_pattern_config.entrypoint
112-
)
111+
try:
112+
entrypoint_schema = next(
113+
ep['parameterSchema']
114+
for ep in contract_schemas['entrypoints']
115+
if ep['name'] == operation_pattern_config.entrypoint
116+
)
117+
except StopIteration as e:
118+
raise ConfigurationError(
119+
f'Contract `{contract_config.address}` has no entrypoint `{operation_pattern_config.entrypoint}`'
120+
) from e
121+
113122
entrypoint_schema_path = join(parameter_schemas_path, f'{operation_pattern_config.entrypoint}.json')
114123

115124
if not exists(entrypoint_schema_path):

src/dipdup/config.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,10 @@ def __post_init_post_parse__(self):
395395
_logger.info('Substituting index templates')
396396
for index_name, index_config in self.indexes.items():
397397
if isinstance(index_config, IndexTemplateConfig):
398-
template = self.templates[index_config.template]
398+
try:
399+
template = self.templates[index_config.template]
400+
except KeyError as e:
401+
raise ConfigurationError(f'Template `{index_config.template}` not found in `templates` config section') from e
399402
raw_template = json.dumps(template, default=pydantic_encoder)
400403
for key, value in index_config.values.items():
401404
value_regex = r'<[ ]*' + key + r'[ ]*>'
@@ -410,17 +413,26 @@ def __post_init_post_parse__(self):
410413
for index_config in self.indexes.values():
411414
if isinstance(index_config, OperationIndexConfig):
412415
if isinstance(index_config.datasource, str):
413-
index_config.datasource = self.datasources[index_config.datasource]
416+
try:
417+
index_config.datasource = self.datasources[index_config.datasource]
418+
except KeyError as e:
419+
raise ConfigurationError(f'Datasource `{index_config.datasource}` not found in `datasources` config section') from e
414420

415421
for i, contract in enumerate(index_config.contracts):
416422
if isinstance(contract, str):
417-
index_config.contracts[i] = self.contracts[contract]
423+
try:
424+
index_config.contracts[i] = self.contracts[contract]
425+
except KeyError as e:
426+
raise ConfigurationError(f'Contract `{contract}` not found in `contracts` config section') from e
418427

419428
for handler in index_config.handlers:
420429
callback_patterns[handler.callback].append(handler.pattern)
421430
for pattern in handler.pattern:
422431
if isinstance(pattern.destination, str):
423-
pattern.destination = self.contracts[pattern.destination]
432+
try:
433+
pattern.destination = self.contracts[pattern.destination]
434+
except KeyError as e:
435+
raise ConfigurationError(f'Contract `{pattern.destination}` not found in `contracts` config section') from e
424436

425437
elif isinstance(index_config, BigMapIndexConfig):
426438
if isinstance(index_config.datasource, str):
@@ -430,7 +442,10 @@ def __post_init_post_parse__(self):
430442
callback_patterns[handler.callback].append(handler.pattern)
431443
for pattern in handler.pattern:
432444
if isinstance(pattern.contract, str):
433-
pattern.contract = self.contracts[pattern.contract]
445+
try:
446+
pattern.contract = self.contracts[pattern.contract]
447+
except KeyError as e:
448+
raise ConfigurationError(f'Contract `{pattern.contract}` not found in `contracts` config section') from e
434449

435450
else:
436451
raise NotImplementedError(f'Index kind `{index_config.kind}` is not supported')
@@ -474,6 +489,8 @@ def load(
474489
for match in re.finditer(ENV_VARIABLE_REGEX, raw_config):
475490
variable, default_value = match.group(1), match.group(2)
476491
value = env.get(variable)
492+
if not default_value and not value:
493+
raise ConfigurationError(f'Environment variable `{variable}` is not set')
477494
placeholder = '${' + variable + ':-' + default_value + '}'
478495
raw_config = raw_config.replace(placeholder, value or default_value)
479496

src/dipdup/dipdup.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
TzktDatasourceConfig,
2020
)
2121
from dipdup.datasources.tzkt.datasource import TzktDatasource
22+
from dipdup.exceptions import ConfigurationError
2223
from dipdup.hasura import configure_hasura
2324
from dipdup.models import IndexType, State
2425
from dipdup.utils import reindex, tortoise_wrapper
@@ -40,7 +41,11 @@ async def run(self) -> None:
4041
url = self._config.database.connection_string
4142
cache = isinstance(self._config.database, SqliteDatabaseConfig)
4243
models = f'{self._config.package}.models'
43-
rollback_fn = getattr(importlib.import_module(f'{self._config.package}.handlers.{ROLLBACK_HANDLER}'), ROLLBACK_HANDLER)
44+
45+
try:
46+
rollback_fn = getattr(importlib.import_module(f'{self._config.package}.handlers.{ROLLBACK_HANDLER}'), ROLLBACK_HANDLER)
47+
except ModuleNotFoundError as e:
48+
raise ConfigurationError(f'Package `{self._config.package}` not found. Have you forgot to call `init`?') from e
4449

4550
async with tortoise_wrapper(url, models):
4651
await self.initialize_database()

0 commit comments

Comments
 (0)