Commit 0c82bae9 by Calen Pennington

Add the caller to user_api caught error logging

parent 4d21fbe6
...@@ -6,6 +6,7 @@ from collections import defaultdict ...@@ -6,6 +6,7 @@ from collections import defaultdict
from functools import wraps from functools import wraps
import logging import logging
import json import json
import traceback
from django import forms from django import forms
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
...@@ -65,16 +66,19 @@ def intercept_errors(api_error, ignore_errors=None): ...@@ -65,16 +66,19 @@ def intercept_errors(api_error, ignore_errors=None):
LOGGER.warning(msg) LOGGER.warning(msg)
raise raise
caller = traceback.format_stack(limit=2)[0]
# Otherwise, log the error and raise the API-specific error # Otherwise, log the error and raise the API-specific error
msg = ( msg = (
u"An unexpected error occurred when calling '{func_name}' " u"An unexpected error occurred when calling '{func_name}' "
u"with arguments '{args}' and keyword arguments '{kwargs}': " u"with arguments '{args}' and keyword arguments '{kwargs}' from {caller}: "
u"{exception}" u"{exception}"
).format( ).format(
func_name=func.func_name, func_name=func.func_name,
args=args, args=args,
kwargs=kwargs, kwargs=kwargs,
exception=ex.developer_message if hasattr(ex, 'developer_message') else repr(ex) exception=ex.developer_message if hasattr(ex, 'developer_message') else repr(ex),
caller=caller.strip()
) )
LOGGER.exception(msg) LOGGER.exception(msg)
raise api_error(msg) raise api_error(msg)
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Tests for helper functions. Tests for helper functions.
""" """
import json import json
import re
import mock import mock
import ddt import ddt
from django import forms from django import forms
...@@ -52,22 +53,35 @@ class InterceptErrorsTest(TestCase): ...@@ -52,22 +53,35 @@ class InterceptErrorsTest(TestCase):
@mock.patch('openedx.core.djangoapps.user_api.helpers.LOGGER') @mock.patch('openedx.core.djangoapps.user_api.helpers.LOGGER')
def test_logs_errors(self, mock_logger): def test_logs_errors(self, mock_logger):
self.maxDiff = None
exception = 'openedx.core.djangoapps.user_api.tests.test_helpers.FakeInputException' exception = 'openedx.core.djangoapps.user_api.tests.test_helpers.FakeInputException'
expected_log_msg = ( expected_log_msg = (
u"An unexpected error occurred when calling 'intercepted_function' with arguments '()' and " u"An unexpected error occurred when calling 'intercepted_function' with arguments '()' and "
u"keyword arguments '{'raise_error': <class '" + exception + u"'>}': FakeInputException()" u"keyword arguments '{{'raise_error': <class '{}'>}}' "
) u"from File \"{}\", line XXX, in test_logs_errors\n"
u" intercepted_function(raise_error=FakeInputException): FakeInputException()"
).format(exception, __file__)
# Verify that the raised exception has the error message # Verify that the raised exception has the error message
try: try:
intercepted_function(raise_error=FakeInputException) intercepted_function(raise_error=FakeInputException)
except FakeOutputException as ex: except FakeOutputException as ex:
self.assertEqual(ex.message, expected_log_msg) actual_message = re.sub(r'line \d+', 'line XXX', ex.message, flags=re.MULTILINE)
self.assertEqual(actual_message, expected_log_msg)
# Verify that the error logger is called # Verify that the error logger is called
# This will include the stack trace for the original exception # This will include the stack trace for the original exception
# because it's called with log level "ERROR" # because it's called with log level "ERROR"
mock_logger.exception.assert_called_once_with(expected_log_msg) calls = mock_logger.exception.mock_calls
self.assertEqual(len(calls), 1)
name, args, kwargs = calls[0]
self.assertEqual(name, '')
self.assertEqual(len(args), 1)
self.assertEqual(kwargs, {})
actual_message = re.sub(r'line \d+', 'line XXX', args[0], flags=re.MULTILINE)
self.assertEqual(actual_message, expected_log_msg)
class FormDescriptionTest(TestCase): class FormDescriptionTest(TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment