✨ Handle user not found

This commit is contained in:
nikolark committed 2022-04-17 21:13:22 +02:00
1 parent ce2cdfd409
commit 95d7d13f38
5 files changed
+53 -3

No files matched your search

+15 -1
View File
@@ -1,4 +1,6 @@
import strawberry
from django.core.exceptions import ObjectDoesNotExist
from strawberry.extensions import Extension
from bbs.api.mutations.shop import ShopMutations
from bbs.api.mutations.users import UsersMutations
@@ -17,4 +19,16 @@ class Mutation(ShopMutations, UsersMutations):
pass
schema = strawberry.Schema(query=Query, mutation=Mutation)
class HandleNotFound(Extension):
def on_request_end(self):
result = self.execution_context.result
if result.errors:
for error in result.errors:
if isinstance(error.original_error, ObjectDoesNotExist):
error.extensions = {
"code": "NOT_FOUND",
"model": type(error.original_error).__qualname__.replace(".DoesNotExist", ""),
}
schema = strawberry.Schema(query=Query, mutation=Mutation, extensions=[HandleNotFound])