-
Notifications
You must be signed in to change notification settings - Fork 765
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
import logging | ||
|
||
from django.utils.log import log_response | ||
|
||
logger = logging.getLogger("django.graphene") | ||
|
||
|
||
class ClientErrorLogMiddleware: | ||
""" | ||
Logs graphql requests 4xx errors. (Except 401, 403) | ||
""" | ||
|
||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
response = self.get_response(request) | ||
|
||
try: | ||
if ( | ||
400 <= response.status_code < 500 | ||
and response.status_code not in (401, 403) | ||
and "graphql" in request.path.lower() | ||
): | ||
response_json = json.loads(response.content) | ||
|
||
if "errors" in response_json: | ||
log_response( | ||
message=( | ||
f"Graphql Error: {response_json['errors']}\n" | ||
f"The Query is: {json.loads(request.body)}" | ||
), | ||
response=response, | ||
) | ||
except Exception: | ||
logger.error(f"Error logging graphql error.", exc_info=True) | ||
|
||
return response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import json | ||
import logging | ||
import graphene | ||
import mock | ||
from django.http.response import HttpResponse | ||
from django.test import RequestFactory | ||
from graphene.test import Client | ||
|
||
from .models import Reporter | ||
from .. import DjangoObjectType | ||
from ..middlewares import ClientErrorLogMiddleware | ||
|
||
|
||
class ReporterType(DjangoObjectType): | ||
class Meta: | ||
model = Reporter | ||
fields = "__all__" | ||
|
||
|
||
class Query(graphene.ObjectType): | ||
reporter = graphene.Field(ReporterType) | ||
|
||
def resolve_reporter(self, info, **args): | ||
return Reporter.objects.first() | ||
|
||
|
||
def test_should_log_error(caplog): | ||
Reporter.objects.create(last_name="ABA") | ||
|
||
invalid_query = """ | ||
query ReporterQuery { | ||
reporter { | ||
invalidAttrName | ||
} | ||
} | ||
""" | ||
|
||
schema = graphene.Schema(query=Query) | ||
client = Client(schema) | ||
response = client.execute(invalid_query) | ||
|
||
factory = RequestFactory() | ||
request = factory.post( | ||
"/graphql", data=json.dumps(invalid_query), content_type="application/json" | ||
) | ||
|
||
http_res = HttpResponse(json.dumps(response).encode(), status=400) | ||
|
||
get_response = mock.MagicMock() | ||
get_response.return_value = http_res | ||
|
||
middleware = ClientErrorLogMiddleware(get_response) | ||
middleware(request) | ||
|
||
assert len(caplog.records) == 1 | ||
assert caplog.records[0] != "WARNING" | ||
assert str(response["errors"]) in caplog.text | ||
assert invalid_query in caplog.text | ||
|
||
|
||
def test_should_not_log_success(caplog): | ||
Reporter.objects.create(last_name="ABA") | ||
|
||
valid_query = """ | ||
query ReporterQuery { | ||
reporter { | ||
lastName | ||
} | ||
} | ||
""" | ||
|
||
schema = graphene.Schema(query=Query) | ||
client = Client(schema) | ||
response = client.execute(valid_query) | ||
|
||
factory = RequestFactory() | ||
request = factory.post( | ||
"/graphql", data=json.dumps(valid_query), content_type="application/json" | ||
) | ||
|
||
http_res = HttpResponse(json.dumps(response).encode(), status=200) | ||
|
||
get_response = mock.MagicMock() | ||
get_response.return_value = http_res | ||
|
||
middleware = ClientErrorLogMiddleware(get_response) | ||
middleware(request) | ||
|
||
assert len(caplog.records) == 0 | ||
|
||
|
||
def test_should_not_log_non_graphql_error(caplog): | ||
factory = RequestFactory() | ||
request = factory.post( | ||
"/users", data=json.dumps({"name": "Mario"}), content_type="application/json" | ||
) | ||
http_res = HttpResponse( | ||
json.dumps({"errors": ["Got to be Luigi"]}).encode(), status=400 | ||
) | ||
|
||
get_response = mock.MagicMock() | ||
get_response.return_value = http_res | ||
|
||
middleware = ClientErrorLogMiddleware(get_response) | ||
middleware(request) | ||
|
||
assert len(caplog.records) == 0 |
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.
Add ClientErrorLogMiddleware for detailed 4xx error logging #1128
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
base: main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Add ClientErrorLogMiddleware for detailed 4xx error logging #1128
Changes from all commits
54bb937
1f46804
163dfb8
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.