Skip to content

Commit 0cb202a

Browse files
committed
detailed exc meta
1 parent 1c8e855 commit 0cb202a

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

fastapi_jsonapi/data_layers/sqla_orm.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,30 @@ async def update_object(
361361
has_updated = True
362362
try:
363363
await self.save()
364+
except IntegrityError:
365+
log.exception("Could not update object with data update %s", data_update)
366+
msg = "Object update error"
367+
raise BadRequest(
368+
msg,
369+
pointer="/data",
370+
meta={
371+
"type": self.type_,
372+
"id": view_kwargs.get(self.url_id_field),
373+
},
374+
)
364375
except DBAPIError as e:
365376
await self.session.rollback()
366377

367-
err_message = f"Got an error {e.__class__.__name__} during update data in DB"
378+
err_message = f"Got an error {e.__class__.__name__} during updating obj {view_kwargs} data in DB"
368379
log.error(err_message, exc_info=e)
369380

370381
raise InternalServerError(
371382
detail=err_message,
372383
pointer="/data",
384+
meta={
385+
"type": self.type_,
386+
"id": view_kwargs.get(self.url_id_field),
387+
},
373388
)
374389

375390
await self.after_update_object(obj=obj, model_kwargs=new_data, view_kwargs=view_kwargs)
@@ -390,12 +405,16 @@ async def delete_object(self, obj: TypeModel, view_kwargs: dict):
390405
except DBAPIError as e:
391406
await self.session.rollback()
392407

393-
err_message = f"Got an error {e.__class__.__name__} during update data in DB"
408+
err_message = f"Got an error {e.__class__.__name__} deleting object {view_kwargs}"
394409
log.error(err_message, exc_info=e)
395410

396411
raise InternalServerError(
397412
detail=err_message,
398413
pointer="/data",
414+
meta={
415+
"type": self.type_,
416+
"id": view_kwargs.get(self.url_id_field),
417+
},
399418
)
400419

401420
await self.after_delete_object(obj, view_kwargs)

fastapi_jsonapi/exceptions/json_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from http import HTTPStatus
44
from typing import (
5+
Any,
56
List,
67
Optional,
78
Union,
@@ -26,6 +27,7 @@ def __init__(
2627
title: Optional[str] = None,
2728
status_code: Optional[int] = None,
2829
errors: Optional[List["HTTPException"]] = None,
30+
meta: Optional[dict[str, Any]] = None,
2931
):
3032
"""
3133
Init base HTTP exception.
@@ -36,6 +38,7 @@ def __init__(
3638
:param status_code: the HTTP status code applicable to this problem
3739
:param title: a short, human-readable summary of the problem
3840
:param errors: may be passed over other arguments as list of `HTTPException` objects
41+
:param meta: default meta
3942
"""
4043
if status_code is not None:
4144
self.status_code = status_code
@@ -44,7 +47,7 @@ def __init__(
4447
self.title = title
4548

4649
self.source = None
47-
self.meta = None
50+
self.meta = meta
4851
self._detail = detail
4952

5053
parameter = parameter or self.parameter

tests/test_api/test_api_sqla_with_includes.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,14 +1304,18 @@ async def test_fail_to_bind_relationship_with_constraint(
13041304
url = app.url_path_for("get_user_bio_detail", obj_id=user_1_bio.id)
13051305
url = f"{url}?include=user"
13061306
res = await client.patch(url, json=patch_user_bio_body)
1307-
assert res.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR, res.text
1307+
assert res.status_code == status.HTTP_400_BAD_REQUEST, res.text
13081308
assert res.json() == {
13091309
"errors": [
13101310
{
1311-
"detail": "Got an error IntegrityError during update data in DB",
1311+
"detail": "Object update error",
13121312
"source": {"pointer": "/data"},
1313-
"status_code": status.HTTP_500_INTERNAL_SERVER_ERROR,
1314-
"title": "Internal Server Error",
1313+
"status_code": status.HTTP_400_BAD_REQUEST,
1314+
"title": "Bad Request",
1315+
"meta": {
1316+
"id": str(user_1_bio.id),
1317+
"type": "user_bio",
1318+
},
13151319
},
13161320
],
13171321
}

0 commit comments

Comments
 (0)