-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from mgxd/enh/helpers
ENH: Add helper function to send breadcrumb on interp termination
- Loading branch information
Showing
9 changed files
with
143 additions
and
31 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
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from __future__ import annotations | ||
|
||
import atexit | ||
import sys | ||
|
||
from migas.operations import add_project | ||
|
||
|
||
def track_exit(project: str, version: str, error_funcs: dict | None = None) -> None: | ||
atexit.register(_final_breadcrumb, project, version, error_funcs) | ||
|
||
def _final_breadcrumb(project: str, version: str, error_funcs: dict | None = None) -> dict: | ||
kwargs = _inspect_error(error_funcs) | ||
return add_project(project, version, **kwargs) | ||
|
||
|
||
def _inspect_error(error_funcs: dict | None) -> dict: | ||
etype, evalue, etb = None, None, None | ||
|
||
# Python 3.12, new method | ||
# MG: Cannot reproduce behavior while testing with 3.12.0 | ||
# if hasattr(sys, 'last_exc'): | ||
# etype, evalue, etb = sys.last_exc | ||
|
||
# < 3.11 | ||
if hasattr(sys, 'last_type'): | ||
etype = sys.last_type | ||
evalue = sys.last_value | ||
etb = sys.last_traceback | ||
|
||
if etype: | ||
ename = etype.__name__ | ||
|
||
if isinstance(error_funcs, dict) and ename in error_funcs: | ||
func = error_funcs[ename] | ||
kwargs = func(etype, evalue, etb) | ||
|
||
elif ename in ('KeyboardInterrupt', 'BdbQuit'): | ||
kwargs = { | ||
'status': 'S', | ||
'status_desc': 'Suspended', | ||
} | ||
|
||
else: | ||
kwargs = { | ||
'status': 'F', | ||
'status_desc': 'Errored', | ||
'error_type': ename, | ||
'error_desc': evalue, | ||
} | ||
else: | ||
kwargs = { | ||
'status': 'C', | ||
'status_desc': 'Completed', | ||
} | ||
|
||
return kwargs |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pytest | ||
|
||
import migas | ||
|
||
TEST_ROOT = "http://localhost:8080/" | ||
TEST_ENDPOINT = f"{TEST_ROOT}graphql" | ||
|
||
|
||
|
||
@pytest.fixture(scope='module') | ||
def setup_migas(): | ||
"""Ensure migas is configured to communicate with the staging app.""" | ||
migas.setup(endpoint=TEST_ENDPOINT) | ||
|
||
assert migas.config.Config._is_setup |
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,42 @@ | ||
import sys | ||
|
||
import pytest | ||
|
||
import migas | ||
|
||
|
||
class CustomException(Exception): | ||
... | ||
|
||
|
||
def sample_error_func(etype: Exception, evalue: str, etb: str): | ||
ename = etype.__name__ | ||
if ename == "CustomException": | ||
return { | ||
'status': 'F', | ||
'status_desc': 'Custom Error!', | ||
'error_type': ename, | ||
'error_desc': 'Custom Error!', | ||
} | ||
|
||
|
||
@pytest.mark.parametrize('error_funcs,error,status,error_desc', [ | ||
(None, None, 'C', None), | ||
(None, KeyboardInterrupt, 'S', None), | ||
(None, KeyError, 'F', 'KeyError: \'foo\''), | ||
({'CustomException': sample_error_func}, CustomException, 'F', 'Custom Error!'), | ||
]) | ||
def test_inspect_error(monkeypatch, error_funcs, error, status, error_desc): | ||
|
||
# do not actually call the server | ||
if error is not None: | ||
monkeypatch.setattr(sys, 'last_type', error, raising=False) | ||
monkeypatch.setattr(sys, 'last_value', error_desc, raising=False) | ||
monkeypatch.setattr(sys, 'last_traceback', 'Traceback...', raising=False) | ||
|
||
from migas.helpers import _inspect_error | ||
res = _inspect_error(error_funcs) | ||
|
||
assert res.get('status') == status | ||
if error_desc is not None: | ||
assert res.get('error_desc') == error_desc |
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 |
---|---|---|
|
@@ -12,5 +12,4 @@ def _check_server_available() -> bool: | |
return False | ||
return True | ||
|
||
|
||
do_server_tests = _check_server_available() |