Skip to content

Commit 53fe6f9

Browse files
fsecada01claude
andauthored
Remove @logger.catch decorators and make loguru an optional dependency (#8)
@logger.catch silently swallows exceptions and returns None, preventing proper error propagation to callers. This is unsuitable for production where frameworks need exceptions to flow for proper HTTP responses, retries, and observability. The existing try/except blocks already provide targeted error handling with session rollback and explicit logger.error() calls where needed. Loguru is now an optional dependency (pip install sqlmodel_crud_utils[loguru]) so teams using their own logging/telemetry can use stdlib logging instead. The logger in utils.py falls back to logging.getLogger() when loguru is not installed. https://claude.ai/code/session_01G7xKgBPQia81rZHBU6BxCG Co-authored-by: Claude <noreply@anthropic.com>
1 parent a103531 commit 53fe6f9

4 files changed

Lines changed: 21 additions & 25 deletions

File tree

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ classifiers = [
7171
"Operating System :: OS Independent",
7272
]
7373
dependencies = [
74-
"loguru>=0.7.3",
7574
"python-dateutil>=2.9.0.post0",
7675
"python-dotenv>=1.1.0",
7776
"sqlmodel>=0.0.24",
7877
]
78+
79+
[project.optional-dependencies]
80+
loguru = ["loguru>=0.7.3"]
7981
[project.urls]
8082
"Homepage" = "https://fsecada01.github.io/SQLModel-CRUD-Utilities/sqlmodel_crud_utils.html"
8183
"Repository" = "https://github.com/fsecada01/sqlmodel_crud_utils"

sqlmodel_crud_utils/a_sync.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44

55
from dateutil.parser import parse as date_parse
66
from dotenv import load_dotenv
7-
from loguru import logger
87
from sqlalchemy.exc import MultipleResultsFound
98
from sqlalchemy.orm import lazyload, selectinload
109
from sqlmodel import SQLModel, select
1110
from sqlmodel.ext.asyncio.session import AsyncSession
1211
from sqlmodel.sql.expression import SelectOfScalar
1312

14-
from sqlmodel_crud_utils.utils import get_sql_dialect_import, get_val, is_date
13+
from sqlmodel_crud_utils.utils import (
14+
get_sql_dialect_import,
15+
get_val,
16+
is_date,
17+
logger,
18+
)
1519

1620
load_dotenv() # take environment variables from .env.
1721

1822
upsert = get_sql_dialect_import(dialect=get_val("SQL_DIALECT"))
1923

2024

21-
@logger.catch
2225
async def get_result_from_query(query: SelectOfScalar, session: AsyncSession):
2326
"""
2427
Processes an SQLModel query object and returns a singular result from the
@@ -40,7 +43,6 @@ async def get_result_from_query(query: SelectOfScalar, session: AsyncSession):
4043
return results
4144

4245

43-
@logger.catch
4446
async def get_one_or_create(
4547
session_inst: AsyncSession,
4648
model: type[SQLModel],
@@ -90,7 +92,6 @@ async def _get_entry(sqlmodel, **key_args):
9092
return created, False
9193

9294

93-
@logger.catch
9495
async def write_row(data_row: Type[SQLModel], session_inst: AsyncSession):
9596
"""
9697
Writes a new instance of an SQLModel ORM model to the database, with an
@@ -115,7 +116,6 @@ async def write_row(data_row: Type[SQLModel], session_inst: AsyncSession):
115116
return False, None
116117

117118

118-
@logger.catch
119119
async def insert_data_rows(data_rows, session_inst: AsyncSession):
120120
"""
121121
@@ -157,7 +157,6 @@ async def insert_data_rows(data_rows, session_inst: AsyncSession):
157157
return status, {"success": processed_rows, "failed": failed_rows}
158158

159159

160-
@logger.catch
161160
async def get_row(
162161
id_str: str or int,
163162
session_inst: AsyncSession,
@@ -204,7 +203,6 @@ async def get_row(
204203
return success, row
205204

206205

207-
@logger.catch
208206
async def get_rows(
209207
session_inst: AsyncSession,
210208
model: type[SQLModel],
@@ -417,7 +415,6 @@ async def get_rows(
417415
return success, results
418416

419417

420-
@logger.catch
421418
async def get_rows_within_id_list(
422419
id_str_list: list[str | int],
423420
session_inst: AsyncSession,
@@ -443,7 +440,6 @@ async def get_rows_within_id_list(
443440
return success, results
444441

445442

446-
@logger.catch
447443
async def delete_row(
448444
id_str: str or int,
449445
session_inst: AsyncSession,
@@ -481,7 +477,6 @@ async def delete_row(
481477
return success
482478

483479

484-
@logger.catch
485480
async def bulk_upsert_mappings(
486481
payload: list,
487482
session_inst: AsyncSession,
@@ -514,7 +509,6 @@ async def bulk_upsert_mappings(
514509
return True, results.all()
515510

516511

517-
@logger.catch
518512
async def update_row(
519513
id_str: int | str,
520514
data: dict,

sqlmodel_crud_utils/sync.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44

55
from dateutil.parser import parse as date_parse
66
from dotenv import load_dotenv
7-
from loguru import logger
87
from sqlalchemy.exc import MultipleResultsFound
98
from sqlalchemy.orm import lazyload, selectinload
109
from sqlmodel import Session, SQLModel, select
1110
from sqlmodel.sql.expression import SelectOfScalar
1211

13-
from sqlmodel_crud_utils.utils import get_sql_dialect_import, get_val, is_date
12+
from sqlmodel_crud_utils.utils import (
13+
get_sql_dialect_import,
14+
get_val,
15+
is_date,
16+
logger,
17+
)
1418

1519
load_dotenv() # take environment variables from .env.
1620

1721
upsert = get_sql_dialect_import(dialect=get_val("SQL_DIALECT"))
1822

1923

20-
@logger.catch
2124
def get_result_from_query(query: SelectOfScalar, session: Session):
2225
"""
2326
Processes an SQLModel query object and returns a singular result from the
@@ -39,7 +42,6 @@ def get_result_from_query(query: SelectOfScalar, session: Session):
3942
return results
4043

4144

42-
@logger.catch
4345
def get_one_or_create(
4446
session_inst: Session,
4547
model: type[SQLModel],
@@ -89,7 +91,6 @@ def _get_entry(sqlmodel, **key_args):
8991
return created, False
9092

9193

92-
@logger.catch
9394
def write_row(data_row: Type[SQLModel], session_inst: Session):
9495
"""
9596
Writes a new instance of an SQLModel ORM model to the database, with an
@@ -114,7 +115,6 @@ def write_row(data_row: Type[SQLModel], session_inst: Session):
114115
return False, None
115116

116117

117-
@logger.catch
118118
def insert_data_rows(data_rows, session_inst: Session):
119119
"""
120120
@@ -154,7 +154,6 @@ def insert_data_rows(data_rows, session_inst: Session):
154154
return status, {"success": processed_rows, "failed": failed_rows}
155155

156156

157-
@logger.catch
158157
def get_row(
159158
id_str: str or int,
160159
session_inst: Session,
@@ -201,7 +200,6 @@ def get_row(
201200
return success, row
202201

203202

204-
@logger.catch
205203
def get_rows(
206204
session_inst: Session,
207205
model: type[SQLModel],
@@ -414,7 +412,6 @@ def get_rows(
414412
return success, results
415413

416414

417-
@logger.catch
418415
def get_rows_within_id_list(
419416
id_str_list: list[str | int],
420417
session_inst: Session,
@@ -444,7 +441,6 @@ def get_rows_within_id_list(
444441
return success, results
445442

446443

447-
@logger.catch
448444
def delete_row(
449445
id_str: str or int,
450446
session_inst: Session,
@@ -482,7 +478,6 @@ def delete_row(
482478
return success
483479

484480

485-
@logger.catch
486481
def bulk_upsert_mappings(
487482
payload: list,
488483
session_inst: Session,
@@ -515,7 +510,6 @@ def bulk_upsert_mappings(
515510
return True, results.all()
516511

517512

518-
@logger.catch
519513
def update_row(
520514
id_str: int | str,
521515
data: dict,

sqlmodel_crud_utils/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import importlib
2+
import logging
23
import os
34

45
from dateutil.parser import parse as date_parse
56

7+
try:
8+
from loguru import logger
9+
except ImportError:
10+
logger = logging.getLogger("sqlmodel_crud_utils")
11+
612

713
def get_val(val: str):
814
"""

0 commit comments

Comments
 (0)