Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ serve the queries.
url(r'^graphql$', GraphQLView.as_view(graphiql=True)),
]

handler500 = 'graphene_django.views.server_error'

Examples
--------

Expand Down
14 changes: 14 additions & 0 deletions graphene_django/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json

import pytest
from mock import patch
import django.http

try:
from urllib import urlencode
Expand Down Expand Up @@ -558,3 +560,15 @@ def test_passes_request_into_context_request(client):

assert response.status_code == 200
assert response_json(response) == {"data": {"request": "testing"}}


def test_response_500(client):
with pytest.raises(RuntimeError), patch('graphql.backend.core.execute_and_validate', side_effect=RuntimeError):
response = client.get(url_string(query="{request}", q="testing"))


def test_response_500_handler(client):
import graphene_django.views
response = graphene_django.views.server_error(django.http.HttpRequest())
assert response.status_code == 500
assert response_json(response) == {"errors": ["server error"]}
2 changes: 2 additions & 0 deletions graphene_django/tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
url(r"^graphql/batch", GraphQLView.as_view(batch=True)),
url(r"^graphql", GraphQLView.as_view(graphiql=True)),
]

handler500 = 'graphene_django.views.server_error'
29 changes: 17 additions & 12 deletions graphene_django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

import six
from django.http import HttpResponse, HttpResponseNotAllowed
from django.http.response import HttpResponseBadRequest
from django.http.response import HttpResponseBadRequest, HttpResponseServerError
from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views.generic import View
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.decorators.csrf import ensure_csrf_cookie, requires_csrf_token

from graphql import get_default_backend
from graphql.error import format_error as format_graphql_error
from graphql.error import GraphQLError
from graphql.error import GraphQLError, GraphQLSyntaxError
from graphql.execution import ExecutionResult
from graphql.type.schema import GraphQLSchema

Expand Down Expand Up @@ -245,10 +245,10 @@ def execute_graphql_request(
return None
raise HttpError(HttpResponseBadRequest("Must provide query string."))

backend = self.get_backend(request)
try:
backend = self.get_backend(request)
document = backend.document_from_string(self.schema, query)
except Exception as e:
except GraphQLSyntaxError as e:
return ExecutionResult(errors=[e], invalid=True)

if request.method.lower() == "get":
Expand All @@ -266,13 +266,13 @@ def execute_graphql_request(
)
)

try:
extra_options = {}
if self.executor:
# We only include it optionally since
# executor is not a valid argument in all backends
extra_options["executor"] = self.executor
extra_options = {}
if self.executor:
# We only include it optionally since
# executor is not a valid argument in all backends
extra_options["executor"] = self.executor

try:
return document.execute(
root=self.get_root_value(request),
variables=variables,
Expand All @@ -281,7 +281,7 @@ def execute_graphql_request(
middleware=self.get_middleware(request),
**extra_options
)
except Exception as e:
except GraphQLError as e:
return ExecutionResult(errors=[e], invalid=True)

@classmethod
Expand Down Expand Up @@ -338,3 +338,8 @@ def get_content_type(request):
meta = request.META
content_type = meta.get("CONTENT_TYPE", meta.get("HTTP_CONTENT_TYPE", ""))
return content_type.split(";", 1)[0].lower()


@requires_csrf_token
def server_error(request, template_name=None):
return HttpResponseServerError('{"errors":["server error"]}', content_type='application/json')