Skip to content
This repository was archived by the owner on Nov 2, 2022. It is now read-only.

very-very first version decorator-based exception catch API (Introduce HandlerChain) #18

Closed
wants to merge 5 commits into from

Conversation

WindSoilder
Copy link
Contributor

I have tried to implement with the way using HandlerChain (which is close to the last proposol inside #5 I think.)
But for now it's just handle for the simlest case: the handled exc is not an instance of ExceptionGroup. (the exc can be an instance of ExceptionGroup, but the output traceback information is not so clearly.)
Sorry for the disturb......I'm afraid my solution is too bad to go on :(
So I submit this PR, hope it can help us to see the new picture.

=======================================
For the actual changes:
It's a little different to the last proposol, which is:

with open_handler() as handler:
    @handler.handle(RuntimeError)
    def handler1(exc):
        ...

In the PR here, the usage is:

handler = HandlerChain()
with handler:
    @handler.handle(RuntimeError)
    def handler1(exc):
        ...

After a little thought about it, I think if we use HandlerChain, we can re-use the chain when we need it, so we don't need to re-define function inside every with block. And we can write code like this:

# a.py
handler = HandlerChain()

@handler.handle(RuntimeError)
def handler1(exc):
    ...

@handler.handle(ValueError)
def handler2(exc):
    ...


# b.py
with handler:
    ...

Note that there are decisions make by the handler:

  1. when code inside with handler raise Exception, the handler will always raise an instance of ExceptionGroup.
  2. If the handler re-raised exception, we will not see the traceback in-side the handler, instead, we will show the actual user code. The same thing happened with context property.

I have add the test_script named handler.py, we can run the script to see what's going on there :)

@WindSoilder WindSoilder requested a review from njsmith February 13, 2019 01:52
@codecov
Copy link

codecov bot commented Feb 13, 2019

Codecov Report

Merging #18 into master will increase coverage by 2.35%.
The diff coverage is 76.63%.

@@            Coverage Diff             @@
##           master      #18      +/-   ##
==========================================
+ Coverage   66.55%   68.91%   +2.35%     
==========================================
  Files          11       12       +1     
  Lines         311      415     +104     
  Branches       28       34       +6     
==========================================
+ Hits          207      286      +79     
- Misses        101      124      +23     
- Partials        3        5       +2
Impacted Files Coverage Δ
exceptiongroup/_tests/test_scripts/handler.py 0% <0%> (ø)
exceptiongroup/_tests/test_tools.py 100% <100%> (ø) ⬆️
exceptiongroup/__init__.py 100% <100%> (ø) ⬆️
exceptiongroup/_tools.py 69.15% <90.24%> (+13.09%) ⬆️

@WindSoilder WindSoilder closed this by deleting the head repository Apr 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant