-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils: added decorator to simplify common error handling
- we add a simple decorator which allows arguments for the exceptions to catch and also for the exception to later reraise from the caught exception - this simplifies the code within the version manager when we deal with versions that could be integer strings
- Loading branch information
Showing
2 changed files
with
47 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
karapace - Test utils | ||
Copyright (c) 2024 Aiven Ltd | ||
See LICENSE for details | ||
""" | ||
|
||
from karapace.utils import catch_and_raise_error | ||
|
||
import pytest | ||
|
||
|
||
def test_catch_and_raise_error(): | ||
class RaiseMe(Exception): | ||
pass | ||
|
||
@catch_and_raise_error(to_catch=(ValueError,), to_raise=RaiseMe) | ||
def v(): | ||
int("not a number") | ||
|
||
with pytest.raises(RaiseMe): | ||
v() | ||
|
||
@catch_and_raise_error(to_catch=(ZeroDivisionError,), to_raise=RaiseMe) | ||
def z(): | ||
_ = 100 / 0 | ||
|
||
with pytest.raises(RaiseMe): | ||
z() |