transitions
transitions copied to clipboard
Collect error messages from failed conditions
Currently, the may_<trigger>() methods return a simple boolean indicating whether a trigger is allowed given the current state and its conditions.
I wonder if it would be useful to allow collecting a list of messages about which conditions failed.
For example:
flow.may_install()
# Returns: False
# Proposed behavior:
flow.may_install(collect_fails=True)
# Returns:
[
{"condition": "has_installation_report", "message": "Please upload an installation report first."},
]
# or maybe even the complete list of conditions
[
{"condition": "has_installation_report", "result": False, "message": "Please upload an installation report first."},
{"condition": "has_quality_approval", "result": True, "message": ""}
]
Benefits:
- Provides actionable diagnostics for UI/UX or API feedback without raising exceptions.
- Helps developers debug state machine failures more efficiently.
- Keeps business logic (conditions) self-contained while making transitions introspectable.
- Default behavior remains backward-compatible, returning a simple boolean.
After reading the documentation thoroughly, I noticed that all arguments passed to trigger or may_<trigger> are forwarded to all evaluated conditions and callbacks. And this allows me to pass any error messages through exceptions, which is not a terrible design choice.