From 5b415a1de66025a8cb6a7a74796dafb5529f6264 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sat, 3 Oct 2015 12:24:51 -0700 Subject: [PATCH] Improved Django view exception handling --- graphene/contrib/django/views.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/graphene/contrib/django/views.py b/graphene/contrib/django/views.py index a9212e89..a2f813e1 100644 --- a/graphene/contrib/django/views.py +++ b/graphene/contrib/django/views.py @@ -2,6 +2,7 @@ import json from django.http import JsonResponse from django.views.generic import View +from django.conf import settings from graphql.core.error import GraphQLError, format_error @@ -14,6 +15,7 @@ def form_error(error): class GraphQLView(View): schema = None + @staticmethod def format_result(result): data = {'data': result.data} @@ -34,6 +36,8 @@ class GraphQLView(View): result = self.schema.execute(query, root=object()) data = self.format_result(result) except Exception, e: + if settings.DEBUG: + raise e data = { "errors": [{"message": str(e)}] } @@ -41,13 +45,14 @@ class GraphQLView(View): return JsonResponse(data) def get(self, request, *args, **kwargs): - query = request.GET.get('query') or request.GET.get('q') or '' - return self.execute_query(request, query) + query = request.GET.get('query') + return self.execute_query(request, query or '') def post(self, request, *args, **kwargs): if request.body: received_json_data = json.loads(request.body) - query = received_json_data.get('query') or '' + query = received_json_data.get('query') else: - query = request.POST.get('query') or request.POST.get('q') - return self.execute_query(request, query) + query = request.POST.get('query') or request.GET.get('query') + raise Exception(query) + return self.execute_query(request, query or '')