tortoise / tortoise/tortoise-orm

Provide more context for DoesNotExist Exception

Open
#741 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

With FastAPI it's possible to register a central exception handler for certain errors.
It's elegant to register a listener for DoesNotExist, because then the error handling doesn't have to be created in every route.
However I'd like to give a proper error message what kind of Model did not exist and with what primary key it was queried.
Currently that is not possible because DoesNotExist doesn't provide the required context.

Ideal implementation:

from tortoise.exceptions import DoesNotExist

 try:
    obj = await MyModel.get(id=1)
except DoesNotExist as e:
    assert e.model is MyModel
    assert e.pk_name == 'id'
    assert e.pk_value == 1

PS: It would be nice if we could rename DoesNotExist to DoesNotExistError

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the tortoise.exceptions.DoesNotExist entry point and trace the MyModel.get(id=1) lookup that raises it. Define completion by making the exception expose the model, primary-key name, and queried value as shown; the requested rename to DoesNotExistError is an additional consideration.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.