-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into potel-base
- Loading branch information
Showing
7 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ pre-commit # local linting | |
httpcore | ||
openfeature-sdk | ||
launchdarkly-server-sdk | ||
typer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,7 @@ | |
"potel", | ||
"pure_eval", | ||
"trytond", | ||
"typer", | ||
], | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import sentry_sdk | ||
from sentry_sdk.utils import ( | ||
capture_internal_exceptions, | ||
event_from_exception, | ||
) | ||
from sentry_sdk.integrations import Integration, DidNotEnable | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Callable | ||
from typing import Any | ||
from typing import Type | ||
from typing import Optional | ||
|
||
from types import TracebackType | ||
|
||
Excepthook = Callable[ | ||
[Type[BaseException], BaseException, Optional[TracebackType]], | ||
Any, | ||
] | ||
|
||
try: | ||
import typer | ||
except ImportError: | ||
raise DidNotEnable("Typer not installed") | ||
|
||
|
||
class TyperIntegration(Integration): | ||
identifier = "typer" | ||
|
||
@staticmethod | ||
def setup_once(): | ||
# type: () -> None | ||
typer.main.except_hook = _make_excepthook(typer.main.except_hook) # type: ignore | ||
|
||
|
||
def _make_excepthook(old_excepthook): | ||
# type: (Excepthook) -> Excepthook | ||
def sentry_sdk_excepthook(type_, value, traceback): | ||
# type: (Type[BaseException], BaseException, Optional[TracebackType]) -> None | ||
integration = sentry_sdk.get_client().get_integration(TyperIntegration) | ||
|
||
# Note: If we replace this with ensure_integration_enabled then | ||
# we break the exceptiongroup backport; | ||
# See: https://github.com/getsentry/sentry-python/issues/3097 | ||
if integration is None: | ||
return old_excepthook(type_, value, traceback) | ||
|
||
with capture_internal_exceptions(): | ||
event, hint = event_from_exception( | ||
(type_, value, traceback), | ||
client_options=sentry_sdk.get_client().options, | ||
mechanism={"type": "typer", "handled": False}, | ||
) | ||
sentry_sdk.capture_event(event, hint=hint) | ||
|
||
return old_excepthook(type_, value, traceback) | ||
|
||
return sentry_sdk_excepthook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import pytest | ||
|
||
pytest.importorskip("typer") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import subprocess | ||
import sys | ||
from textwrap import dedent | ||
import pytest | ||
|
||
from typer.testing import CliRunner | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_catch_exceptions(tmpdir): | ||
app = tmpdir.join("app.py") | ||
|
||
app.write( | ||
dedent( | ||
""" | ||
import typer | ||
from unittest import mock | ||
from sentry_sdk import init, transport | ||
from sentry_sdk.integrations.typer import TyperIntegration | ||
def capture_envelope(self, envelope): | ||
print("capture_envelope was called") | ||
event = envelope.get_event() | ||
if event is not None: | ||
print(event) | ||
transport.HttpTransport.capture_envelope = capture_envelope | ||
init("http://foobar@localhost/123", integrations=[TyperIntegration()]) | ||
app = typer.Typer() | ||
@app.command() | ||
def test(): | ||
print("test called") | ||
raise Exception("pollo") | ||
app() | ||
""" | ||
) | ||
) | ||
|
||
with pytest.raises(subprocess.CalledProcessError) as excinfo: | ||
subprocess.check_output([sys.executable, str(app)], stderr=subprocess.STDOUT) | ||
|
||
output = excinfo.value.output | ||
|
||
assert b"capture_envelope was called" in output | ||
assert b"test called" in output | ||
assert b"pollo" in output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters