Skip to content

Automate browser based workflows with AI

fix(SKY-12269): return 422 not 500 for malformed update_workflow body (#7306)

Aaron Perez3dfd788fbfe2
diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.py
index cc48556e2..af5f64f6c 100644
--- a/skyvern/forge/sdk/routes/agent_protocol.py
+++ b/skyvern/forge/sdk/routes/agent_protocol.py
@@ -1244,7 +1244,7 @@ async def update_workflow(
raise HTTPException(status_code=422, detail=format_yaml_error(exc))
except WorkflowDefinitionValidationException as e:
raise e
- except (SkyvernHTTPException, ValidationError) as e:
+ except (HTTPException, SkyvernHTTPException, ValidationError) as e:
# Bubble up well-formed client errors so they are not converted to 500s
raise e
except Exception as e:
diff --git a/tests/unit/test_update_workflow_route.py b/tests/unit/test_update_workflow_route.py
new file mode 100644
index 000000000..55763d648
--- /dev/null
+++ b/tests/unit/test_update_workflow_route.py
@@ -0,0 +1,60 @@
+"""Regression tests for the ``update_workflow`` route's error handling.
+
+A ``POST /v1/workflows/{workflow_id}`` body carrying neither ``yaml_definition`` nor
+``json_definition`` is a client error and must return 422. The inline ``HTTPException(422)``
+used to be swallowed by the handler's catch-all ``except Exception`` and re-wrapped as a 500
+(``FailedToUpdateWorkflow``), tripping the production zero-threshold 5xx monitor.
+"""
+
+from __future__ import annotations
+
+import datetime as dt
+import importlib
+
+import pytest
+from fastapi import FastAPI, Request
+from fastapi.responses import JSONResponse
+from fastapi.testclient import TestClient
+
+from skyvern.exceptions import SkyvernHTTPException
+from skyvern.forge.sdk.routes.routers import base_router
+from skyvern.forge.sdk.schemas.organizations import Organization
+from skyvern.forge.sdk.services import org_auth_service
+
+ORG_ID = "o_test"
+
+
+def _make_org() -> Organization:
+ now = dt.datetime.now(dt.timezone.utc)
+ return Organization(
+ organization_id=ORG_ID,
+ organization_name="Test Org",
+ created_at=now,
+ modified_at=now,
+ )
+
+
[email protected](scope="module")
+def client() -> TestClient:
+ importlib.import_module("skyvern.forge.sdk.routes.agent_protocol")
+
+ app = FastAPI()
+ app.include_router(base_router, prefix="/v1")
+
+ # Mirror api_app.py so a raised SkyvernHTTPException renders as its own status code
+ # (e.g. the pre-fix FailedToUpdateWorkflow would render as 500 here, not be re-raised).
+ @app.exception_handler(SkyvernHTTPException)
+ async def _handle_skyvern_http_exception(request: Request, exc: SkyvernHTTPException) -> JSONResponse:
+ return JSONResponse(status_code=exc.status_code, content={"detail": exc.message})
+
+ app.dependency_overrides[org_auth_service.get_current_org] = _make_org
+ app.dependency_overrides[org_auth_service.get_current_user_id_or_none] = lambda: None
+
+ return TestClient(app)
+
+
+def test_update_workflow_without_definition_returns_422(client: TestClient) -> None:
+ resp = client.post("/v1/workflows/wpid_test", json={})
+
+ assert resp.status_code == 422, resp.text
+ assert "json" in resp.json()["detail"].lower()