[RFC] Database Query Exception Handling #2516
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Premise
In ~65% of our DB queries we are only looking for a single row. We use
Query.first()
to execute the query and return the first row. However that return type is nullable, since the result could be empty. We have these categories of use cases for handling that null:!
assert it and move on.cord-api-v3/src/components/user/user.repository.ts
Lines 211 to 212 in 08c2f0a
NotFoundException
with a custom message (bad user input)cord-api-v3/src/components/user/unavailability/unavailability.service.ts
Lines 100 to 105 in 08c2f0a
ServerException
with a custom message (bad DB query or something)cord-api-v3/src/components/user/user.repository.ts
Lines 101 to 103 in 08c2f0a
cord-api-v3/src/components/user/user.repository.ts
Lines 351 to 354 in 08c2f0a
cord-api-v3/src/components/user/user.repository.ts
Line 99 in 08c2f0a
cord-api-v3/src/components/story/story.service.ts
Lines 59 to 66 in 08c2f0a
Similar to above however if create fails to set some data that the read query is expecting, the read query could return no rows, and in this case we know that problem is actually with the create query.
Analysis
Esp during dev & query authoring this could happen.
I believe in the beginning, when this pattern was established, we weren't capturing stack traces correctly on database queries. Now we do, so a standardized/generic throw could still give enough info to debug.
mutation createUser
->{ code: "Server", message: "Failed to create user" }
mutation createUser
->{ code: "Server", message: "Server Error" }
I think we can just infer the first error message. If we are worried about what is shown to users, that should probably be a static custom message in the UI codebase.
On the flip side, is the first error message nicer? Yes.
It's also could be possible to just do this automatically based on the mutation name.
Also something to think about for both this and # 3 is if they are ever hit. These are unexpected server errors, I'd like to think that this is a very rare occurrence.
Solution?
exactlyOne()
will work here too. Custom message handling can be addressed in # 4┆Issue is synchronized with this Monday item by Unito