-
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.
feat: add a semantic name for internal exception for a better dev exp…
…erience on unwrap_or_return + @early_return feature
- Loading branch information
1 parent
b0d015d
commit 49b1616
Showing
7 changed files
with
59 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from meiga.error import Error | ||
|
||
if TYPE_CHECKING: | ||
from meiga.alias import AnyResult | ||
|
||
|
||
class WaitingForEarlyReturn(Error): | ||
result: "AnyResult" | ||
|
||
def __init__(self, result: "AnyResult") -> None: | ||
self.result = result | ||
Exception.__init__(self) | ||
|
||
def __str__(self) -> str: | ||
return ( | ||
f"This exception wraps the following result -> {self.result}" | ||
f"\nIf you want to handle this error and return a Failure, please use early_return decorator on your function." | ||
f"\nMore info about how to use unwrap_or_return in combination with @early_return decorator on https://alice-biometrics.github.io/meiga/usage/result/#unwrap_or_return" | ||
) | ||
|
||
def __repr__(self) -> str: | ||
return str(self) |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,30 @@ | ||
import pytest | ||
|
||
from meiga import Error, Result | ||
from meiga.failures import WaitingForEarlyReturn | ||
|
||
|
||
def expected_error(value: str) -> str: | ||
return ( | ||
f"This exception wraps the following result -> Result[status: failure | value: {value}]" | ||
f"\nIf you want to handle this error and return a Failure, please use early_return decorator on your function." | ||
f"\nMore info about how to use unwrap_or_return in combination with @early_return decorator on https://alice-biometrics.github.io/meiga/usage/result/#unwrap_or_return" | ||
) | ||
|
||
|
||
@pytest.mark.unit | ||
class TestWaitingForEarlyReturn: | ||
def should_str_as_expected_default_error(self): | ||
result = Result(failure=Error()) | ||
|
||
exception = WaitingForEarlyReturn(result) | ||
|
||
assert expected_error("Error") == str(exception) | ||
|
||
def should_str_as_expected_an_exception(self): | ||
wrapped_exception = ValueError("Something went wrong") | ||
result = Result(failure=wrapped_exception) | ||
|
||
exception = WaitingForEarlyReturn(result) | ||
|
||
assert expected_error(wrapped_exception.__repr__()) == str(exception) |