Custom server error response

This commit is contained in:
Marco Paolini 2019-08-12 12:39:22 +01:00
parent 60164d8e7a
commit df863aa250
2 changed files with 9 additions and 2 deletions

View File

@ -55,6 +55,8 @@ serve the queries.
url(r'^graphql$', GraphQLView.as_view(graphiql=True)),
]
handler500 = 'graphene_django.views.server_error'
Examples
--------

View File

@ -4,11 +4,11 @@ import re
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
@ -338,3 +338,8 @@ class GraphQLView(View):
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')