Skip to content

Commit 035d065

Browse files
committed
perf: improve exception handling in Sentry
1 parent 23a788f commit 035d065

File tree

1 file changed

+55
-28
lines changed

1 file changed

+55
-28
lines changed

renku/cli/_exc.py

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959

6060
import click
6161

62+
_BUG = click.style(
63+
'Ahhhhhhhh! You have found a bug. 🐞\n\n',
64+
fg='red',
65+
bold=True,
66+
)
67+
6268
HAS_SENTRY = None
6369
SENTRY_DSN = os.getenv('SENTRY_DSN')
6470

@@ -91,41 +97,62 @@ def __init__(self, *args, **kwargs):
9197
def main(self, *args, **kwargs):
9298
"""Catch all exceptions."""
9399
try:
94-
return super().main(*args, **kwargs)
100+
result = super().main(*args, **kwargs)
101+
return result
95102
except Exception:
96103
if HAS_SENTRY:
97-
from sentry_sdk import capture_exception
98-
click.echo('Sentry event ID: ' + capture_exception(), err=True)
99-
raise
104+
self._handle_sentry()
100105

101106
if not (sys.stdin.isatty() and sys.stdout.isatty()):
102107
raise
103108

104-
value = click.prompt(
105-
click.style(
106-
'Ahhhhhhhh! You have found a bug. 🐞\n\n',
107-
fg='red',
108-
bold=True,
109-
) + click.style(
110-
'1. Open an issue by typing "open";\n',
111-
fg='green',
112-
) + click.style(
113-
'2. Print human-readable information by typing '
114-
'"print";\n',
115-
fg='yellow',
116-
) + click.style(
117-
'3. See the full traceback without submitting details '
118-
'(default: "ignore").\n\n',
119-
fg='red',
120-
) + 'Please select an action by typing its name',
121-
type=click.Choice([
122-
'open',
123-
'print',
124-
'ignore',
125-
], ),
126-
default='ignore',
109+
self._handle_github()
110+
111+
def _handle_sentry(self):
112+
"""Handle exceptions using Sentry."""
113+
from sentry_sdk import capture_exception, configure_scope
114+
from sentry_sdk.utils import capture_internal_exceptions
115+
116+
with configure_scope() as scope:
117+
with capture_internal_exceptions():
118+
from git import Repo
119+
from renku.cli._git import get_git_home
120+
from renku.models.datasets import Author
121+
122+
user = Author.from_git(Repo(get_git_home()))
123+
124+
scope.user = {'name': user.name, 'email': user.email}
125+
126+
event_id = capture_exception()
127+
click.echo(
128+
_BUG + 'Recorded in Sentry with ID: {0}\n'.format(event_id),
129+
err=True,
127130
)
128-
getattr(self, '_process_' + value)()
131+
raise
132+
133+
def _handle_github(self):
134+
"""Handle exception and submit it as GitHub issue."""
135+
value = click.prompt(
136+
_BUG + click.style(
137+
'1. Open an issue by typing "open";\n',
138+
fg='green',
139+
) + click.style(
140+
'2. Print human-readable information by typing '
141+
'"print";\n',
142+
fg='yellow',
143+
) + click.style(
144+
'3. See the full traceback without submitting details '
145+
'(default: "ignore").\n\n',
146+
fg='red',
147+
) + 'Please select an action by typing its name',
148+
type=click.Choice([
149+
'open',
150+
'print',
151+
'ignore',
152+
], ),
153+
default='ignore',
154+
)
155+
getattr(self, '_process_' + value)()
129156

130157
def _format_issue_title(self):
131158
"""Return formatted title."""

0 commit comments

Comments
 (0)