refactor: name draft diagnostic codes

This commit is contained in:
lda
2026-06-28 03:06:17 +07:00 Verified
parent 8324a6b3f0
commit 58092966ee
3 changed files with 60 additions and 7 deletions
+5 -2
View File
@@ -14,6 +14,9 @@ from wf_core.models.schemas import NodeDef
from .models import WorkflowDraftWorkspace, summarize_draft_workspace
from .store import DraftWorkspaceConflictError, DraftWorkspaceStore
WORKSPACE_EXISTS_CODE = "workspace_exists"
REVISION_CONFLICT_CODE = "revision_conflict"
JsonObject = dict[str, Any]
JsonPatch = list[dict[str, Any]]
NodeDefsForDraft = Callable[[JsonObject], Sequence[NodeDef]]
@@ -47,7 +50,7 @@ def create_draft_workspace(
except DraftWorkspaceConflictError as exc:
return _conflict_payload(
exc.workspace,
code="workspace_exists",
code=WORKSPACE_EXISTS_CODE,
message=f"draft workspace {workspace_id!r} already exists",
)
return summarize_draft_workspace(workspace)
@@ -139,7 +142,7 @@ def _revision_conflict_payload(
) -> JsonObject:
return _conflict_payload(
workspace,
code="revision_conflict",
code=REVISION_CONFLICT_CODE,
message=(
f"workspace {workspace.id!r} is at revision "
f"{workspace.revision}, not {expected_revision}"
+10 -5
View File
@@ -16,6 +16,11 @@ from wf_core.validation.issues import ValidationIssue, ValidationIssueCode
from .adapter import build_workflow_from_draft
from .models import WorkflowDraft
DRAFT_INVALID_CODE = "draft_invalid"
PATCH_INVALID_CODE = "patch_invalid"
DRAFT_NOT_OBJECT_CODE = "draft_not_object"
UNKNOWN_OUTCOME_CODE = "unknown_outcome"
JsonObject = dict[str, Any]
JsonPatch = list[dict[str, Any]]
OutcomeLookup = Callable[[str], tuple[str, ...] | None]
@@ -89,7 +94,7 @@ def patch_workflow_draft(
except Exception as exc:
return _invalid_result(
DraftDiagnostic(
code="patch_invalid",
code=PATCH_INVALID_CODE,
path="patch",
message=str(exc),
)
@@ -97,7 +102,7 @@ def patch_workflow_draft(
if not isinstance(patched, dict):
return _invalid_result(
DraftDiagnostic(
code="draft_not_object",
code=DRAFT_NOT_OBJECT_CODE,
path="",
message="patched draft must be a JSON object",
)
@@ -124,12 +129,12 @@ def _diagnostic_from_exception(exc: Exception) -> DraftDiagnostic:
if isinstance(exc, ValidationError):
error = exc.errors()[0]
return DraftDiagnostic(
code="draft_invalid",
code=DRAFT_INVALID_CODE,
path=_format_location(error["loc"]),
message=error["msg"],
)
return DraftDiagnostic(
code="draft_invalid",
code=DRAFT_INVALID_CODE,
path="",
message=str(exc),
)
@@ -237,7 +242,7 @@ def _validate_known_outcomes(
for outcome in route_map:
if outcome not in known_outcomes:
return DraftDiagnostic(
code="unknown_outcome",
code=UNKNOWN_OUTCOME_CODE,
path=f"routes.{step_id}.{outcome}",
step_id=step_id,
message=(