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

prepare func raises Exception altough on_exception is defined #626

Closed
match1 opened this issue Aug 9, 2023 · 1 comment
Closed

prepare func raises Exception altough on_exception is defined #626

match1 opened this issue Aug 9, 2023 · 1 comment
Assignees
Labels

Comments

@match1
Copy link

match1 commented Aug 9, 2023

Describe the bug
When using a _can_trigger: If a prepare callback raises an Exception, this exception gets propangated to the calling code even if you define an on_exception callback.

Minimal working example

from transitions.core import Machine


class Amp:
    def __init__(self):
        self._external_resource: None | str = None
        self.machine = Machine(
            self,
            send_event=True,
            states=[
                "silent",
                "loud",
            ],
            transitions=[
                {
                    "trigger": "turn_up",
                    "source": "silent",
                    "dest": "loud",
                    "prepare": "fetch_external_resource",
                    # "conditions": "is_external_resource_ready",
                },
            ],
            initial="silent",
            on_exception="on_exception",
        )


    def fetch_external_resource(self, event):
        print("Refresh something..")
        self._external_resource = "something"
        # but this can fail
        raise ValueError("Something went wrong")
    
    def on_exception(self, event):
        print(f"Handling exception correctly {event.error=!r}")

amp = Amp()
amp.turn_up()
print (f"State is still {amp.state}")
try:
    amp.may_turn_up()
except ValueError:
    print("An exception got raised altough we defined on_exception")

Expected behavior
The on_exception callback gets called when you use may_turn_up()? At least thats what I would expect.

@aleneum
Copy link
Member

aleneum commented May 9, 2024

Hello @match1,

thank you very much for taking the time to report the bug report and creating the MWE. Much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants