From 6eb3c2f84199800ca2dafd0702c3d2d75567000c Mon Sep 17 00:00:00 2001 From: disqualifier Date: Sun, 9 Aug 2026 02:18:08 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20log-XOR-raise=20=E2=80=94=20demote=20the?= =?UTF-8?q?=20pre-raise=20log=20to=20DEBUG,=20revise=20the=20contract=20(D?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit every wrapped method did log.exception (ERROR + traceback) immediately before re-raising the driver error — the same failure reported at two layers (the log AND the raised exception). that is the log-and-raise violation: a method that re-raises must not also error-log, because the caller — which alone knows whether the failure is fatal or routine — is the one that logs. demote all wrapped-method logs to log.debug(..., exc_info=True): the traceback stays available at DEBUG, and the raised exception is the single loud terminal signal. docstring updated from "logs via getLogger and re-raises" to the corrected raise-XOR-log contract. no behavior change beyond log level — the fail-loud re-raise is unchanged. Signed-off-by: disqualifier --- src/mysql/mysql.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/mysql/mysql.py b/src/mysql/mysql.py index bfffbe6..d8386c1 100644 --- a/src/mysql/mysql.py +++ b/src/mysql/mysql.py @@ -17,8 +17,11 @@ a literal `%` in layer-2 SQL text must be written `%%` only when params are pass 1 handles this for you. errors are FAIL LOUD (unlike the mongo lib's swallow-and-default): every method catches -the driver error, logs via getLogger(__name__), and re-raises. a None/[] return is only -ever a real result, never a swallowed failure. for anything not wrapped, use `.pool`. +the driver error and re-raises it - the raised exception IS the signal, and the caller +(which alone knows whether a failure is fatal or routine) decides and logs. the wrapped +method itself logs only at DEBUG (with the traceback) so a failure isn't reported twice +(raise XOR log). a None/[] return is only ever a real result, never a swallowed failure. +for anything not wrapped, use `.pool`. """ import asyncio @@ -135,7 +138,7 @@ class MysqlDB: await cur.execute("SELECT 1") await cur.fetchone() except _DRIVER_ERRORS: - log.exception("mysql.connect() failed") + log.debug("mysql.connect() failed", exc_info=True) if pool is not None: pool.close() await pool.wait_closed() @@ -166,7 +169,7 @@ class MysqlDB: self._pool.close() await self._pool.wait_closed() except _DRIVER_ERRORS: - log.exception("mysql.close()") + log.debug("mysql.close()", exc_info=True) raise finally: self._pool = None @@ -229,7 +232,7 @@ class MysqlDB: try: rowcount, _ = await self._run(query, params) except _DRIVER_ERRORS: - log.exception("mysql.execute(): %s", query) + log.debug("mysql.execute(): %s", query, exc_info=True) raise return rowcount @@ -238,7 +241,7 @@ class MysqlDB: try: return await self._fetchall(query, params) except _DRIVER_ERRORS: - log.exception("mysql.fetch(): %s", query) + log.debug("mysql.fetch(): %s", query, exc_info=True) raise async def fetchone(self, query: str, params: Optional[Sequence] = None) -> Optional[dict]: @@ -246,7 +249,7 @@ class MysqlDB: try: return await self._fetchone(query, params) except _DRIVER_ERRORS: - log.exception("mysql.fetchone(): %s", query) + log.debug("mysql.fetchone(): %s", query, exc_info=True) raise async def fetchval(self, query: str, params: Optional[Sequence] = None) -> Any: @@ -254,7 +257,7 @@ class MysqlDB: try: row = await self._fetchone(query, params) except _DRIVER_ERRORS: - log.exception("mysql.fetchval(): %s", query) + log.debug("mysql.fetchval(): %s", query, exc_info=True) raise if not row: return None @@ -276,7 +279,7 @@ class MysqlDB: try: await self._run(f"CREATE DATABASE IF NOT EXISTS {_quote_ident_literal(name)}") except _DRIVER_ERRORS: - log.exception("mysql.create_database(%s)", name) + log.debug("mysql.create_database(%s)", name, exc_info=True) raise async def create_table(self, name: str, schema: Dict[str, str]) -> None: @@ -292,7 +295,7 @@ class MysqlDB: try: await self._run(f"CREATE TABLE IF NOT EXISTS {_quote_ident_literal(name)} ({cols})") except _DRIVER_ERRORS: - log.exception("mysql.create_table(%s)", name) + log.debug("mysql.create_table(%s)", name, exc_info=True) raise async def drop(self, name: str, *, table: bool = True) -> None: @@ -301,7 +304,7 @@ class MysqlDB: try: await self._run(f"DROP {kind} IF EXISTS {_quote_ident_literal(name)}") except _DRIVER_ERRORS: - log.exception("mysql.drop(%s)", name) + log.debug("mysql.drop(%s)", name, exc_info=True) raise async def insert(self, table: str, values: Dict[str, Any]) -> int: @@ -318,7 +321,7 @@ class MysqlDB: try: _, lastrowid = await self._run(query, list(values.values())) except _DRIVER_ERRORS: - log.exception("mysql.insert(%s)", table) + log.debug("mysql.insert(%s)", table, exc_info=True) raise return lastrowid @@ -333,7 +336,7 @@ class MysqlDB: try: return await self._fetchall(query, params) except _DRIVER_ERRORS: - log.exception("mysql.get(%s)", table) + log.debug("mysql.get(%s)", table, exc_info=True) raise async def get_one(self, table: str, conditions: Optional[Dict[str, Any]] = None) -> Optional[dict]: @@ -343,7 +346,7 @@ class MysqlDB: try: return await self._fetchone(query, params) except _DRIVER_ERRORS: - log.exception("mysql.get_one(%s)", table) + log.debug("mysql.get_one(%s)", table, exc_info=True) raise async def delete(self, table: str, conditions: Optional[Dict[str, Any]] = None) -> int: @@ -356,7 +359,7 @@ class MysqlDB: try: rowcount, _ = await self._run(query, params) except _DRIVER_ERRORS: - log.exception("mysql.delete(%s)", table) + log.debug("mysql.delete(%s)", table, exc_info=True) raise return rowcount @@ -367,7 +370,7 @@ class MysqlDB: try: row = await self._fetchone(query, params) except _DRIVER_ERRORS: - log.exception("mysql.exists(%s)", table) + log.debug("mysql.exists(%s)", table, exc_info=True) raise return bool(_first_value(row)) if row else False @@ -390,7 +393,7 @@ class MysqlDB: try: rowcount, _ = await self._run(query, list(values.values())) except _DRIVER_ERRORS: - log.exception("mysql.upsert(%s)", table) + log.debug("mysql.upsert(%s)", table, exc_info=True) raise return rowcount