-
-
Notifications
You must be signed in to change notification settings - Fork 400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added error for lookup by primary key #759
Conversation
There is |
This one is only thrown when an item gets accessed by primary key. That way it is possible to register a central error handler (e.g. in FastAPI) and e.g. {
"item": "MyModel",
"id": 99,
"msg": "MyModel with id 99 does not exist"
} which is way better than just {
"msg": "Object not found"
} Additionally it is not necessary any more to handle the ObjectNotFoundError in every route. Imho it makes sense to raise different errors for accessing an item by primary key and for filtering and searching an item. |
I have a FastApi application where the user interacts with the db. @router.get(
path="/filter",
response_model=List[MyModelApiOut],
status_code=status.HTTP_200_OK,
)
async def get_models_of_type(type: int, category: int):
type = await MyModelType[type]
category= await MyModelCategory[category]
return [MyModelApiOut.from_orm(k) for k in await MyModel.filter(type=type, category=category)] Without this PR I have to do this: @router.get(
path="/filter",
response_model=List[MyModelApiOut],
status_code=status.HTTP_200_OK,
)
async def get_models_of_type(type: int, category: int):
type = await MyModelType.get_or_none(pk=type)
if type is None:
return Response404(pk_name='id', pk_value=type, msg=f'MyModelType with id {type} does not exist')
category = await MyModelCategory.get_or_none(pk=category)
if category is None:
return Response404(pk_name='id', pk_value=category, msg=f'MyModelCategory with id {category} does not exist')
return [MyModelApiOut.from_orm(k) for k in await MyModel.filter(type=type, category=category)] I have to explicitly check for every item that get's passed in by the user and issue a corresponding error message. I really think differentiating between accessing and item with the primary key and searching for an item with the filter function is a smart design choice and I would really like to see tortoise-orm go that way. |
@long2ice |
@spacemanspiff2007 would you like to update this PR? But probably need to additionally inherit this error from |
fa1f547
to
f3e441e
Compare
# Conflicts: # tortoise/exceptions.py # tortoise/models.py
@abondar like this? |
Pull Request Test Coverage Report for Build 9459828839Details
💛 - Coveralls |
This PR allows to generate nice error messages with a central exception handler (e.g. in FastAPI) - see #741 for initial idea.
I've changed the exception to
ObjectDoesNotExistError
since the user explicitly specifies a primary key which makes this a little different than the filter function.If this will get merged, I'll add something to the docs on how to use it.
Output: