Skip to content

Automate browser based workflows with AI

Stop logging an expected analytics serving-table absence as an error (#7824)

Aaron Perez575dab274fb0
diff --git a/skyvern/forge/sdk/db/_error_handling.py b/skyvern/forge/sdk/db/_error_handling.py
index d4fbe33e6..d5dd845c4 100644
--- a/skyvern/forge/sdk/db/_error_handling.py
+++ b/skyvern/forge/sdk/db/_error_handling.py
@@ -44,12 +44,18 @@ def register_passthrough_exception(exc_type: type[Exception]) -> None:
_PASSTHROUGH_EXCEPTIONS = (*_PASSTHROUGH_EXCEPTIONS, exc_type)
-def db_operation(operation_name: str, log_errors: bool = True) -> Callable[[Callable[P, R]], Callable[P, R]]:
+def db_operation(
+ operation_name: str,
+ log_errors: bool = True,
+ expected_errors: tuple[type[Exception], ...] = (),
+) -> Callable[[Callable[P, R]], Callable[P, R]]:
"""Decorator that wraps an async function with standardized DB error handling.
- Pass-through exceptions (NotFoundError, ScheduleLimitExceededError, etc.):
logged at WARNING level then re-raised — visible to monitoring but not
treated as infrastructure errors.
+ - expected_errors: logged at DEBUG then re-raised, for conditions the caller
+ already handles as a normal outcome.
- SQLAlchemyError: logged with LOG.error() then re-raised
- Exception: logged with LOG.error() then re-raised
@@ -57,6 +63,11 @@ def db_operation(operation_name: str, log_errors: bool = True) -> Callable[[Call
operation_name: Human-readable name used in log messages for context.
log_errors: Whether to log errors before re-raising. Set to False when
stacked under @read_retry() to avoid duplicate log entries.
+ expected_errors: Exception types this operation's callers treat as an
+ expected, handled outcome rather than a failure. Declaring one keeps
+ the wrapper from asserting an error level the caller contradicts.
+ Empty by default, so every operation is loud unless it opts in — keep
+ a declared set no wider than the caller's own ``except``.
Usage:
@db_operation("get_task")
@@ -77,6 +88,16 @@ def db_operation(operation_name: str, log_errors: bool = True) -> Callable[[Call
if log_errors:
LOG.warning("BusinessLogicError", operation=operation_name, exc_info=True)
raise
+ # Must precede the SQLAlchemyError branch: a declared type is usually a subclass of it.
+ except expected_errors as exc:
+ if log_errors:
+ LOG.debug(
+ "ExpectedError",
+ operation=operation_name,
+ error=str(exc),
+ error_type=type(exc).__name__,
+ )
+ raise
except SQLAlchemyError:
if log_errors:
LOG.exception("SQLAlchemyError", operation=operation_name)
diff --git a/tests/unit/test_db_operation_decorator.py b/tests/unit/test_db_operation_decorator.py
index 8277905da..71bf51993 100644
--- a/tests/unit/test_db_operation_decorator.py
+++ b/tests/unit/test_db_operation_decorator.py
@@ -1,9 +1,9 @@
"""Tests for the @db_operation decorator."""
-from unittest.mock import patch
+from unittest.mock import ANY, patch
import pytest
-from sqlalchemy.exc import SQLAlchemyError
+from sqlalchemy.exc import OperationalError, ProgrammingError, SQLAlchemyError
from skyvern.forge.sdk.db._error_handling import db_operation
from skyvern.forge.sdk.db.exceptions import NotFoundError
@@ -174,6 +174,77 @@ async def test_db_operation_log_errors_false_suppresses_logging() -> None:
mock_log.exception.assert_not_called()
+class _ExpectedErrorDB:
+ """Declares one condition its callers handle themselves; everything else stays loud."""
+
+ @db_operation("declared_op", expected_errors=(ProgrammingError,))
+ async def raise_declared(self) -> None:
+ raise ProgrammingError("SELECT 1", {}, Exception("relation does not exist"))
+
+ @db_operation("declared_op", expected_errors=(ProgrammingError,))
+ async def raise_undeclared(self) -> None:
+ raise OperationalError("SELECT 1", {}, Exception("connection does not exist"))
+
+
+async def test_db_operation_declared_expected_error_logs_debug_not_error() -> None:
+ db = _ExpectedErrorDB()
+ with patch("skyvern.forge.sdk.db._error_handling.LOG") as mock_log:
+ with pytest.raises(ProgrammingError):
+ await db.raise_declared()
+ mock_log.debug.assert_called_once_with(
+ "ExpectedError",
+ operation="declared_op",
+ error=ANY,
+ error_type="ProgrammingError",
+ )
+ mock_log.exception.assert_not_called()
+ mock_log.error.assert_not_called()
+
+
+async def test_db_operation_undeclared_error_still_logs_exception() -> None:
+ """Declaring one expected condition must not quiet the rest of the operation's failures."""
+ db = _ExpectedErrorDB()
+ with patch("skyvern.forge.sdk.db._error_handling.LOG") as mock_log:
+ with pytest.raises(OperationalError):
+ await db.raise_undeclared()
+ mock_log.exception.assert_called_once_with("SQLAlchemyError", operation="declared_op")
+ mock_log.debug.assert_not_called()
+
+
+async def test_db_operation_declares_nothing_expected_by_default() -> None:
+ """Operations that don't opt in keep logging every failure at error level."""
+
+ class DefaultDB:
+ @db_operation("default_op")
+ async def raise_programming(self) -> None:
+ raise ProgrammingError("SELECT 1", {}, Exception("relation does not exist"))
+
+ db = DefaultDB()
+ with patch("skyvern.forge.sdk.db._error_handling.LOG") as mock_log:
+ with pytest.raises(ProgrammingError):
+ await db.raise_programming()
+ mock_log.exception.assert_called_once_with("SQLAlchemyError", operation="default_op")
+ mock_log.debug.assert_not_called()
+
+
+async def test_db_operation_expected_error_still_reraises_unchanged() -> None:
+ """Callers degrade by catching the exception, so the instance must arrive untouched."""
+ raised = ProgrammingError("SELECT 1", {}, Exception("relation does not exist"))
+
+ class ReraiseDB:
+ @db_operation("reraise_op", expected_errors=(ProgrammingError,))
+ async def raise_declared(self) -> None:
+ raise raised
+
+ with pytest.raises(ProgrammingError) as caught:
+ await ReraiseDB().raise_declared()
+ assert caught.value is raised
+
+
@pytest.mark.asyncio
async def test_db_operation_schedule_limit_exceeded_is_passthrough() -> None:
"""ScheduleLimitExceededError should be treated as business logic, not unexpected."""