Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write more about purposes of the library: exceptions + data classes ⇒ control flow #10

Open
anatoly-scherbakov opened this issue Dec 9, 2022 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@anatoly-scherbakov
Copy link
Owner

The library documentation refers to a few articles about how exceptions might be used in controlling the control flow of an application. This is an interesting topic to explore further.

I personally very much like to write exceptions as dataclasses and inherit them from DocumentedError. This allows me to encapsulate business logic related information in those exceptions, and then to easily retrieve it in other parts of the application. For instance, I can do something like this:

def ask_hal(question: str, user_name: str):
    try:
        return hal.ask(question)
    except AnswerToAnswer as err:
        raise dataclasses.replace(
            err,
            user_name=user_name,
        )

In this question, we do not provide user_name to the internal layer of the application which is responsible for communication with HAL9000 computer, but we catch the error, enhance it with information available in the API layer, and reraise. Voilà.

Also

ExceptionList, a new feature introduced into Python recently, can be mentioned in relation to this topic.

When?

No idea; perhaps at some point I get inspired by a particular usage example and will get to writing this piece.

@anatoly-scherbakov anatoly-scherbakov added the documentation Improvements or additions to documentation label Dec 9, 2022
@anatoly-scherbakov anatoly-scherbakov self-assigned this Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant