Skip to content

Commit a0e6fcd

Browse files
Fix wrapping unknown exceptions (#214)
1 parent 64c25f7 commit a0e6fcd

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
Please use [this](https://docs.gitlab.com/ee/development/changelog.html) document as guidelines to keep a changelog.
44

5+
## [unreleased]
6+
7+
### Fixed
8+
9+
* cli: Fixed stacktraces missing on exception.
10+
* cli: Fixed wrapping `OSError` with `ConfigurationError` during config loading.
11+
12+
### Changed
13+
14+
* cli: Unknown exceptions are no longer wrapped with `DipDupError`.
15+
516
## 4.1.0 - 2022-01-24
617

718
### Added

src/dipdup/cli.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import atexit
23
import logging
34
import os
45
import signal
@@ -7,6 +8,7 @@
78
from contextlib import AsyncExitStack
89
from contextlib import suppress
910
from dataclasses import dataclass
11+
from functools import partial
1012
from functools import wraps
1113
from os import listdir
1214
from os.path import dirname
@@ -82,15 +84,13 @@ async def wrapper(*args, **kwargs) -> None:
8284
loop = asyncio.get_running_loop()
8385
loop.add_signal_handler(signal.SIGINT, lambda: asyncio.ensure_future(shutdown()))
8486
try:
85-
with DipDupError.wrap():
86-
await fn(*args, **kwargs)
87+
await fn(*args, **kwargs)
8788
except (KeyboardInterrupt, asyncio.CancelledError):
8889
pass
89-
except DipDupError as e:
90-
# FIXME: No traceback in test logs
91-
_logger.critical(e.__repr__())
92-
_logger.info(e.format())
93-
quit(1)
90+
except Exception as e:
91+
help_message = e.format() if isinstance(e, DipDupError) else DipDupError().format()
92+
atexit.register(partial(click.echo, help_message, err=True))
93+
raise
9494

9595
return wrapper
9696

src/dipdup/config.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,6 @@ def load(
10411041
paths: List[str],
10421042
environment: bool = True,
10431043
) -> 'DipDupConfig':
1044-
10451044
current_workdir = os.path.join(os.getcwd())
10461045

10471046
json_config: Dict[str, Any] = {}
@@ -1050,8 +1049,11 @@ def load(
10501049
path = os.path.join(current_workdir, path)
10511050

10521051
_logger.debug('Loading config from %s', path)
1053-
with open(path) as file:
1054-
raw_config = file.read()
1052+
try:
1053+
with open(path) as file:
1054+
raw_config = file.read()
1055+
except OSError as e:
1056+
raise ConfigurationError(str(e))
10551057

10561058
if environment:
10571059
_logger.debug('Substituting environment variables')

src/dipdup/exceptions.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
import textwrap
2-
import traceback
3-
from contextlib import contextmanager
42
from dataclasses import dataclass
53
from dataclasses import field
64
from typing import Any
75
from typing import Dict
8-
from typing import Iterator
96
from typing import Optional
107
from typing import Type
118

12-
import sentry_sdk
139
from tabulate import tabulate
1410
from tortoise.models import Model
1511

1612
from dipdup import spec_version_mapping
1713
from dipdup.enums import ReindexingReason
1814

19-
_tab = '\n\n' + ('_' * 80) + '\n\n'
15+
_tab = ('_' * 80) + '\n\n'
2016

2117

2218
def unindent(text: str) -> str:
@@ -48,6 +44,7 @@ def __repr__(self) -> str:
4844
return f'{self.__class__.__name__}: {self.__doc__}'
4945

5046
def _help(self) -> str:
47+
# TODO: Update guide
5148
return """
5249
Unexpected error occurred!
5350
@@ -61,18 +58,7 @@ def help(self) -> str:
6158
return unindent(self._help())
6259

6360
def format(self) -> str:
64-
exc = f'\n\n{traceback.format_exc()}'.rstrip()
65-
return _tab.join([exc, self.help() + '\n'])
66-
67-
@contextmanager
68-
def wrap(ctx: Optional[Any] = None) -> Iterator[None]:
69-
try:
70-
yield
71-
except DipDupError:
72-
raise
73-
except Exception as e:
74-
sentry_sdk.capture_exception(e)
75-
raise DipDupError from e
61+
return _tab + self.help() + '\n'
7662

7763

7864
@dataclass(frozen=True, repr=False)
@@ -138,7 +124,7 @@ def _help(self) -> str:
138124
],
139125
headers=['', 'spec_version', 'DipDup version'],
140126
)
141-
reindex = _tab + ReindexingRequiredError(ReindexingReason.MIGRATION).help() if self.reindex else ''
127+
reindex = '\n\n' + _tab + ReindexingRequiredError(ReindexingReason.MIGRATION).help() if self.reindex else ''
142128
return f"""
143129
Project migration required!
144130

src/dipdup/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ async def process(self) -> None:
194194
sync_levels = {self.datasource.get_sync_level(s) for s in self._config.subscriptions}
195195
sync_level = sync_levels.pop()
196196
if sync_levels:
197-
raise Exception
197+
raise RuntimeError(f'Multiple sync levels: {sync_levels}')
198198
level = self.state.level
199199

200200
if sync_level is None:

0 commit comments

Comments
 (0)