diff --git a/common/lib/capa/capa/util.py b/common/lib/capa/capa/util.py index 0dfc955c19..d135bafdd8 100644 --- a/common/lib/capa/capa/util.py +++ b/common/lib/capa/capa/util.py @@ -4,6 +4,7 @@ Utility functions for capa. from __future__ import absolute_import import re +import logging import six from cmath import isinf, isnan from decimal import Decimal @@ -18,6 +19,7 @@ from openedx.core.djangolib.markup import HTML # # Utility functions used in CAPA responsetypes default_tolerance = '0.001%' +log = logging.getLogger(__name__) def compare_with_tolerance(student_complex, instructor_complex, tolerance=default_tolerance, relative_tolerance=False): @@ -108,6 +110,24 @@ def contextualize_text(text, context): # private except UnicodeEncodeError: return value.encode('utf8', errors='ignore') + def replace_or_log_error(data_string, old_value, new_value): + """Tries to replace context variable with value and logs exception if there is an error""" + try: + data_string = data_string.replace(old_value, new_value) + return data_string + except Exception as error: + log.exception( + u'ContextualizeTextError: text(%s): %s, context_key(%s): %s, context_value(%s): %s, Error: %s', + type(text), + text, + type(context_key), + context_key, + type(context_value), + context_value, + six.text_type(error) + ) + raise + if not text: return text @@ -121,7 +141,8 @@ def contextualize_text(text, context): # private if context_key in (text.decode('utf-8') if six.PY3 and isinstance(text, bytes) else text): text = convert_to_str(text) context_value = convert_to_str(context[key]) - text = text.replace(context_key, context_value) + text = replace_or_log_error(text, context_key, context_value) + return text diff --git a/common/lib/xmodule/xmodule/capa_base.py b/common/lib/xmodule/xmodule/capa_base.py index bc6f7a8052..0944646079 100644 --- a/common/lib/xmodule/xmodule/capa_base.py +++ b/common/lib/xmodule/xmodule/capa_base.py @@ -620,12 +620,13 @@ class CapaMixin(ScorableXBlockMixin, CapaFields): html = warning try: html += self.lcp.get_html() - except Exception: + except Exception as error: # Couldn't do it. Give up. log.exception( - u"ProblemGetHtmlError: Unable to generate html from LoncapaProblem: !r, !r", + u"ProblemGetHtmlError: Unable to generate html from LoncapaProblem: %r, %r, %s", problem_display_name, - problem_location + problem_location, + text_type(error) ) raise