-
Notifications
You must be signed in to change notification settings - Fork 769
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
Add ClientErrorLogMiddleware for detailed 4xx error logging #1128
base: main
Are you sure you want to change the base?
Add ClientErrorLogMiddleware for detailed 4xx error logging #1128
Conversation
Should this be implemented as graphene middleware? https://docs.graphene-python.org/en/latest/execution/middleware/ |
hmm, I tried implementing with graphene middleware as you suggested @zbyte64 but I cannot make it working. Could you help me with it? logger = logging.getLogger(__name__)
class MyLogMiddleware:
def resolve(self, next, root, info, **args):
res = next(root, info, **args)
if res.errors:
# TODO: build up actual error messages.
logger.warning("3.141592")
return res
def test_test(caplog):
Reporter.objects.create(last_name="ABA")
invalid_query = """
query ReporterQuery {
reporter {
invalidAttrName
}
}
"""
schema = graphene.Schema(query=Query)
schema.execute(invalid_query, middleware=[MyLogMiddleware()])
# THIS FAILS, there is supposed to be one warning logged, but len() returns 0
assert len(caplog.records) == 1 |
It could be that |
I'm sorry, I haven't been able to take much time. However I read Graphene doc, too, but it doesn't seem to elaborate on how errors are handled in graphene middleware. Where should I look if I want to understand? Maybe some file suggestions if you have any? |
@untidy-hair I have a PR on graphql-core repo, i think it can help you how to find how errors are handled: graphql-python/graphql-core-legacy#269 (I'm aware it's not a middleware, just a suggestion 🙂 ) |
@zbyte64 I was able to take some time so I looked into how graphql middleware works but I found that for some errors, middlewares will never run: https://github.com/graphql-python/graphql-core/blob/main/src/graphql/graphql.py#L166-L197. |
Hi, thank you for creating/maintaining this great library!
I've created a PR to add a logging middleware to the library for 4xx errors.
Currently when there is a 400 error, Django's logging is something very simple like this:
I checked actual errors on client side every time this happened and sometimes it was hard to reproduce the error that had happened in the past. I knew it wouldn't be too hard to write a middleware to show more details in the logs, but it had taken me about 2 months before I actually got to write one at work. So I think it would be handy for a lot of people if we bundle one in the library.
Once you enable the middleware (please see docs/installation.rst on how), you will see more details like so:
Thanks in advance :)