Skip to content

Commit

Permalink
WCM-22, WCM-393: Extract "suppress errors during sql query" into sing…
Browse files Browse the repository at this point in the history
…le function

This reduces code duplication, and also allows zeit.web to use this as
a hook to inject "skip if featuretoggle is off" behaviour
  • Loading branch information
wosc committed Sep 26, 2024
1 parent 68ab16b commit 587f19e
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions core/src/zeit/connector/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,11 +589,8 @@ def _search_dav(self, attrlist, expr):

def search_sql(self, query):
result = []
try:
rows = self.session.execute(query).scalars()
except Exception:
log.warning('Error during search_sql, suppressed', exc_info=True)
self.session.rollback()
rows = self._execute_suppress_errors(query)
if rows is None:
return result

for content in rows:
Expand All @@ -613,12 +610,18 @@ def search_sql(self, query):
return result

def search_sql_count(self, query):
rows = self._execute_suppress_errors(query.with_only_columns(sqlalchemy.func.count()))
if rows is None:
return 0
return rows.one()

def _execute_suppress_errors(self, query):
try:
return self.session.execute(query.with_only_columns(sqlalchemy.func.count())).scalar()
return self.session.execute(query).scalars()
except Exception:
log.warning('Error during search_sql_count, suppressed', exc_info=True)
log.warning('Error during search_sql, suppressed', exc_info=True)
self.session.rollback()
return 0
return None

def query(self):
return select(Content)
Expand Down

0 comments on commit 587f19e

Please sign in to comment.