Skip to content
Sarj Code Standards

no-swallowed-exception

Surface caught operational failures instead of recording them only at debug level.

Why

A handler that catches a specific failure, writes it only to a debug or trace log and then continues makes the failure invisible: production log levels drop the record, the caller observes success and no traceback survives. Leaked resources and partial cleanups then go unnoticed.

Fix

Let the error propagate, or handle it explicitly with a typed result the caller must inspect. For deliberate best-effort cleanup, record it at warning or error level with the exception attached, for example logger.exception(...), logger.warning(..., exc_info=True) or logger.opt(exception=error).warning(...).

Examples

Before — flagged A cleanup failure is visible only at debug level
storage/cleanup.py
import logging
from google.api_core.exceptions import GoogleAPIError
logger = logging.getLogger(__name__)
def delete_object(store, path: str) -> None:
try:
store.delete(path=path)
except (GoogleAPIError, OSError) as error:
logger.debug("cleanup unavailable", extra={"error": type(error).__name__})
After — preferred Best-effort cleanup is recorded with the exception attached
storage/cleanup.py
import logging
from google.api_core.exceptions import GoogleAPIError
logger = logging.getLogger(__name__)
def delete_object(store, path: str) -> None:
try:
store.delete(path=path)
except (GoogleAPIError, OSError) as error:
logger.warning("cleanup failed", exc_info=error)