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())