How to hide GraphQL exceptions in strawberry and Django?
I am using GraphQL with strawberry in my Django project. When an exception is raised, the error is shown in the terminal like the server errors. But if a user make mistakes while creating a query, It shouldn't show like this. Here is one of the errors.
GraphQL request:18:5
17 | # }
18 | register(name: "Mahmuud Hasan", email: "test@gmail.com", password: "admin"){
| ^
19 | success
Traceback (most recent call last):
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/graphql/execution/execute.py", line 523, in execute_field
result = resolve_fn(source, info, **args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 778, in _resolver
return _get_result_with_extensions(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 765, in extension_resolver
return reduce(
^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 760, in wrapped_get_result
return _get_result(
^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 717, in _get_result
return field.get_result(
^^^^^^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/types/field.py", line 232, in get_result
return self.base_resolver(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/types/fields/resolver.py", line 250, in __call__
return self.wrapped_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/main/mutations.py", line 47, in register
raise GraphQLError("Email already in use.", extensions={"code": "BAD_USER_INPUT"})
graphql.error.graphql_error.GraphQLError: Email already in use.
I don't want to show this kinds of errors in my terminal. I have tried to make custom error messages and returned as response instead of raising exceptions. But the front-end developer is using Apollo Client and it can't recognize the errors if I don't through an exception.
{
"data": null,
"errors": [
{
"message": "Email already in use.",
"locations": [
{
"line": 18,
"column": 5
}
],
"path": [
"register"
],
"extensions": {
"code": "BAD_USER_INPUT"
}
}
]
}
The response I return from the back-end always goes to the data block but he expects the errors field data, which shows if I raise an exception.
I also tried to use the strawberry.extensions.SchemaExtension to hide the errors. But the errors are printed before the on_operation function runs.
My schema.py code:
import strawberry
from .queries import Query
from .mutations import Mutation
from strawberry.extensions import SchemaExtension
from graphql import GraphQLError
class CustomErrorHandlingExtension(SchemaExtension):
def on_operation(self):
yield
result = self.execution_context.result
print("Execution result:", result)
print("Execution errors:", result.errors if result else "No result")
if result and result.errors:
for error in result.errors:
if not isinstance(error.original_error, GraphQLError):
error.message = "An unexpected error occurred. Please try again later."
error.extensions = {"code": "INTERNAL_SERVER_ERROR"}
schema = strawberry.Schema(query=Query, mutation=Mutation, extensions=[CustomErrorHandlingExtension])
And here is the error response
GraphQL request:18:5
17 | # }
18 | register(name: "Mahmuud Hasan", email: "test@gmail.com", password: "admin"){
| ^
19 | success
Traceback (most recent call last):
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/graphql/execution/execute.py", line 523, in execute_field
result = resolve_fn(source, info, **args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 778, in _resolver
return _get_result_with_extensions(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 765, in extension_resolver
return reduce(
^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 760, in wrapped_get_result
return _get_result(
^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 717, in _get_result
return field.get_result(
^^^^^^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/types/field.py", line 232, in get_result
return self.base_resolver(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/env/lib/python3.12/site-packages/strawberry/types/fields/resolver.py", line 250, in __call__
return self.wrapped_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mahmud/Projects/Alyve/main/mutations.py", line 47, in register
raise GraphQLError("Email already in use.", extensions={"code": "BAD_USER_INPUT"})
graphql.error.graphql_error.GraphQLError: Email already in use.
Execution result: ExecutionResult(data=None, errors=[GraphQLError('Email already in use.', locations=[SourceLocation(line=18, column=5)], path=['register'], extensions={'code': 'BAD_USER_INPUT'})])
Execution errors: [GraphQLError('Email already in use.', locations=[SourceLocation(line=18, column=5)], path=['register'], extensions={'code': 'BAD_USER_INPUT'})]
I also tried to override the report error function, but it doesn't works either.
from strawberry.django.views import GraphQLView
class CustomGraphQLView(GraphQLView):
def get_context(self, request, response):
return {
"request": request,
"response": response
}
def report_errors(self, errors, result):
filtered_errors = []
for error in errors:
if isinstance(error.original_error, GraphQLError):
logger.info(f"GraphQL Validation Error: {error.message}")
else:
filtered_errors.append(error)
if filtered_errors:
super().report_errors(filtered_errors, result)
I can't find proper solution for this issue. How can I hide this errors? Thanks in advanced.