Skip to content

Commit

Permalink
Use TracecatValidationError over ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
daryllimyt committed Jan 26, 2025
1 parent 8e39a61 commit 4d1a71d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
32 changes: 32 additions & 0 deletions tests/unit/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from tracecat.registry.repository import Repository
from tracecat.secrets.models import SecretCreate, SecretKeyValue
from tracecat.secrets.service import SecretsService
from tracecat.types.exceptions import TracecatValidationError


@pytest.fixture
Expand Down Expand Up @@ -321,3 +322,34 @@ def get_secrets(action: BoundRegistryAction) -> list[RegistrySecret]:
"secret_step": "UDF_SECRET_VALUE",
"nested_secret_step": "UDF_SECRET_VALUE",
}


def test_template_action_definition_validates_self_reference():
"""Test that TemplateActionDefinition validates against self-referential steps.
The test verifies:
1. A template action cannot reference itself in its steps
2. The validation error message is descriptive
"""
with pytest.raises(TracecatValidationError) as exc_info:
TemplateActionDefinition(
title="Self Referential Action",
description="This action tries to reference itself",
name="self_ref",
namespace="testing",
display_group="Testing",
expects={},
steps=[
ActionStep(
ref="self_ref_step",
action="testing.self_ref", # This references the template itself
args={},
),
],
returns="${{ steps.self_ref_step.result }}",
)

assert "Steps cannot reference the template action itself: testing.self_ref" in str(
exc_info.value
)
assert "1 steps reference the template action" in str(exc_info.value)
6 changes: 4 additions & 2 deletions tracecat/registry/actions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,14 @@ def validate_steps(self):
# Check for duplicate step refs
if len(step_refs) != len(unique_step_refs):
duplicate_step_refs = [ref for ref in step_refs if step_refs.count(ref) > 1]
raise ValueError(f"Duplicate step references found: {duplicate_step_refs}")
raise TracecatValidationError(
f"Duplicate step references found: {duplicate_step_refs}"
)

# Check if any step action references the template action
template_action = f"{self.namespace}.{self.name}"
if violating_steps := [s for s in self.steps if s.action == template_action]:
raise ValueError(
raise TracecatValidationError(
f"Steps cannot reference the template action itself: {template_action}."
f"{len(violating_steps)} steps reference the template action: {violating_steps}"
)
Expand Down
2 changes: 1 addition & 1 deletion tracecat/types/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, *args, detail: Any | None = None, **kwargs):


class TracecatValidationError(TracecatException):
"""Tracecat user-facting validation error"""
"""Tracecat user-facing validation error"""


class TracecatDSLError(TracecatValidationError):
Expand Down

0 comments on commit 4d1a71d

Please sign in to comment.