Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 74a5d01

Browse files
author
Phil Varner
authored
fix exception logging (#125)
* fix exception logging
1 parent 40a208d commit 74a5d01

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ none
2929

3030
### Fixed
3131

32-
none
32+
- Exception logging
3333

3434
### Security
3535

src/stapi_fastapi/routers/product_router.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import logging
4+
import traceback
45
from typing import TYPE_CHECKING, Self
56

67
from fastapi import APIRouter, HTTPException, Request, Response, status
@@ -22,6 +23,8 @@
2223
if TYPE_CHECKING:
2324
from stapi_fastapi.routers import RootRouter
2425

26+
logger = logging.getLogger(__name__)
27+
2528

2629
class ProductRouter(APIRouter):
2730
def __init__(
@@ -182,7 +185,10 @@ async def search_opportunities(
182185
case Failure(e) if isinstance(e, ConstraintsException):
183186
raise e
184187
case Failure(e):
185-
logging.exception("An error occurred while searching opportunities", e)
188+
logger.error(
189+
"An error occurred while searching opportunities: %s",
190+
traceback.format_exception(e),
191+
)
186192
raise HTTPException(
187193
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
188194
detail="Error searching opportunities",
@@ -221,7 +227,10 @@ async def create_order(
221227
case Failure(e) if isinstance(e, ConstraintsException):
222228
raise e
223229
case Failure(e):
224-
logging.exception("An error occurred while creating order", e)
230+
logger.error(
231+
"An error occurred while creating order: %s",
232+
traceback.format_exception(e),
233+
)
225234
raise HTTPException(
226235
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
227236
detail="Error creating order",

src/stapi_fastapi/routers/root_router.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import traceback
23
from typing import Self
34

45
from fastapi import APIRouter, HTTPException, Request, status
@@ -23,6 +24,8 @@
2324
from stapi_fastapi.responses import GeoJSONResponse
2425
from stapi_fastapi.routers.product_router import ProductRouter
2526

27+
logger = logging.getLogger(__name__)
28+
2629

2730
class RootRouter(APIRouter):
2831
def __init__(
@@ -176,7 +179,10 @@ async def get_orders(self, request: Request) -> OrderCollection:
176179
)
177180
return orders
178181
case Failure(e):
179-
logging.exception("An error occurred while retrieving orders", e)
182+
logger.error(
183+
"An error occurred while retrieving orders: %s",
184+
traceback.format_exception(e),
185+
)
180186
raise HTTPException(
181187
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
182188
detail="Error finding Orders",
@@ -195,8 +201,10 @@ async def get_order(self: Self, order_id: str, request: Request) -> Order:
195201
case Success(Maybe.empty):
196202
raise NotFoundException("Order not found")
197203
case Failure(e):
198-
logging.exception(
199-
f"An error occurred while retrieving order '{order_id}'", e
204+
logger.error(
205+
"An error occurred while retrieving order '%s': %s",
206+
order_id,
207+
traceback.format_exception(e),
200208
)
201209
raise HTTPException(
202210
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
@@ -226,8 +234,9 @@ async def get_order_statuses(
226234
],
227235
)
228236
case Failure(e):
229-
logging.exception(
230-
"An error occurred while retrieving order statuses", e
237+
logger.error(
238+
"An error occurred while retrieving order statuses: %s",
239+
traceback.format_exception(e),
231240
)
232241
raise HTTPException(
233242
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
@@ -243,7 +252,10 @@ async def set_order_status(
243252
case Success(_):
244253
return Response(status_code=status.HTTP_202_ACCEPTED)
245254
case Failure(e):
246-
logging.exception("An error occurred while setting order status", e)
255+
logger.error(
256+
"An error occurred while setting order status: %s",
257+
traceback.format_exception(e),
258+
)
247259
raise HTTPException(
248260
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
249261
detail="Error setting Order Status",

0 commit comments

Comments
 (0)