runtime error

so future outcome error can route to bro
This commit is contained in:
lda
2026-05-07 15:21:34 +07:00 Verified
parent 817792c09f
commit 668a7c21d6
4 changed files with 42 additions and 3 deletions
+4
View File
@@ -23,6 +23,7 @@ from .ops import (
ItemOutput, ItemOutput,
MaybeItemOutput, MaybeItemOutput,
PickKeyInput, PickKeyInput,
RuntimeErrorInput,
SequenceInput, SequenceInput,
TruthyInput, TruthyInput,
ValueOutput, ValueOutput,
@@ -37,6 +38,7 @@ from .ops import (
last_item_or_none, last_item_or_none,
length, length,
pick_key, pick_key,
runtime_error,
truthy, truthy,
) )
from .nodes import ( from .nodes import (
@@ -64,6 +66,7 @@ __all__ = [
"ItemOutput", "ItemOutput",
"MaybeItemOutput", "MaybeItemOutput",
"PickKeyInput", "PickKeyInput",
"RuntimeErrorInput",
"NodeReturn", "NodeReturn",
"NodeSpec", "NodeSpec",
"Nothing", "Nothing",
@@ -97,6 +100,7 @@ __all__ = [
"last_item_or_none", "last_item_or_none",
"length", "length",
"pick_key", "pick_key",
"runtime_error",
"node", "node",
"outcome", "outcome",
"state", "state",
+4
View File
@@ -16,12 +16,14 @@ from .values import (
CoalesceInput, CoalesceInput,
ConstantInput, ConstantInput,
PickKeyInput, PickKeyInput,
RuntimeErrorInput,
TruthyInput, TruthyInput,
ValueOutput, ValueOutput,
coalesce, coalesce,
constant, constant,
default_if_none, default_if_none,
pick_key, pick_key,
runtime_error,
truthy, truthy,
) )
@@ -33,6 +35,7 @@ __all__ = [
"ItemOutput", "ItemOutput",
"MaybeItemOutput", "MaybeItemOutput",
"PickKeyInput", "PickKeyInput",
"RuntimeErrorInput",
"SequenceInput", "SequenceInput",
"TruthyInput", "TruthyInput",
"ValueOutput", "ValueOutput",
@@ -47,5 +50,6 @@ __all__ = [
"last_item_or_none", "last_item_or_none",
"length", "length",
"pick_key", "pick_key",
"runtime_error",
"truthy", "truthy",
] ]
+23 -3
View File
@@ -1,10 +1,10 @@
from __future__ import annotations from __future__ import annotations
from typing import Any from typing import Any, NoReturn
from pydantic import BaseModel from pydantic import BaseModel, Field
from wf_authoring.nodes import NodeReturn, node from wf_authoring.nodes import NodeReturn, Nothing, node
class CoalesceInput(BaseModel): class CoalesceInput(BaseModel):
@@ -39,6 +39,13 @@ class TruthyInput(BaseModel):
value: Any value: Any
class RuntimeErrorInput(BaseModel):
"""Input model for intentionally failing a workflow branch."""
message: str
details: dict[str, Any] = Field(default_factory=dict)
@node( @node(
name="authoring.coalesce", name="authoring.coalesce",
input_model=CoalesceInput, input_model=CoalesceInput,
@@ -92,3 +99,16 @@ def truthy(input: TruthyInput) -> NodeReturn[ValueOutput]:
value = bool(input.value) value = bool(input.value)
outcome = "truthy" if value else "falsey" outcome = "truthy" if value else "falsey"
return NodeReturn(outcome=outcome, output=ValueOutput(value=value)) return NodeReturn(outcome=outcome, output=ValueOutput(value=value))
@node(
name="authoring.runtime_error",
input_model=RuntimeErrorInput,
output_model=Nothing,
description="Raise RuntimeError with the provided message and details.",
)
def runtime_error(input: RuntimeErrorInput) -> NoReturn:
"""Raise RuntimeError with the provided message and details."""
if input.details:
raise RuntimeError(f"{input.message}: {input.details!r}")
raise RuntimeError(input.message)
+11
View File
@@ -18,6 +18,7 @@ from wf_authoring import (
last_item_or_none, last_item_or_none,
length, length,
pick_key, pick_key,
runtime_error,
state_path, state_path,
truthy, truthy,
) )
@@ -258,3 +259,13 @@ def test_truthy_routes_truthy_and_falsey_outcomes() -> None:
assert truthy_result == {"outcome": "truthy", "output": {"value": True}} assert truthy_result == {"outcome": "truthy", "output": {"value": True}}
assert falsey_result == {"outcome": "falsey", "output": {"value": False}} assert falsey_result == {"outcome": "falsey", "output": {"value": False}}
def test_runtime_error_raises_with_message_and_details() -> None:
registry = build_registry(runtime_error)
with pytest.raises(RuntimeError, match="bad branch"):
registry["authoring.runtime_error"](
{"message": "bad branch", "details": {"step": "demo"}},
RuntimeContext(current_node_id="runtime_error"),
)