From 44b738ab7554a54c4255b4900bd4b64b5717523c Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Fri, 13 Dec 2013 15:52:20 -0500 Subject: [PATCH] Have rest_dispatch return JSON when exceptions are thrown. (imported from commit 587a8f46d406c6358480db9e0ebd5afb69e12abf) --- zerver/lib/response.py | 3 +++ zerver/lib/rest.py | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/zerver/lib/response.py b/zerver/lib/response.py index 53dff391e1..5fee1ed65a 100644 --- a/zerver/lib/response.py +++ b/zerver/lib/response.py @@ -34,3 +34,6 @@ def json_success(data={}): def json_error(msg, data={}, status=400): return json_response(res_type="error", msg=msg, data=data, status=status) + +def json_unhandled_exception(): + return json_response(res_type="error", msg="Internal server error", status=500) diff --git a/zerver/lib/rest.py b/zerver/lib/rest.py index a2b71e492a..da643074c2 100644 --- a/zerver/lib/rest.py +++ b/zerver/lib/rest.py @@ -4,10 +4,12 @@ from django.views.decorators.csrf import csrf_exempt, csrf_protect from zerver.decorator import authenticated_json_view, authenticated_rest_api_view, \ process_as_post -from zerver.lib.response import json_method_not_allowed, json_unauthorized +from zerver.lib.response import json_method_not_allowed, json_unauthorized, json_unhandled_exception from django.http import HttpResponseRedirect from django.conf import settings +import logging + METHODS = ('GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH') @@ -77,7 +79,13 @@ def rest_dispatch(request, globals_list, **kwargs): # otherwise we might access and thus cache a value for # request.REQUEST. target_function = process_as_post(target_function) - return target_function(request, **kwargs) + + try: + return target_function(request, **kwargs) + except: + logging.exception('Uncaught exception in rest_dispatch') + return json_unhandled_exception() + return json_method_not_allowed(supported_methods.keys())