Source code for cove.lib.exceptions

import functools
import logging

from django.shortcuts import render
from django.utils.translation import ugettext_lazy as _


logger = logging.getLogger(__name__)


[docs]class CoveInputDataError(Exception): """ An error that we think is due to the data input by the user, rather than a bug in the application. """ def __init__(self, context=None): if context: self.context = context
[docs]class UnrecognisedFileType(CoveInputDataError): context = { 'sub_title': _("Sorry we can't process that data"), 'link': 'index', 'link_text': _('Try Again'), 'msg': _('We did not recognise the file type.\n\nWe can only process json, csv and xlsx files.') }
[docs]def cove_web_input_error(func): @functools.wraps(func) def wrapper(request, *args, **kwargs): try: return func(request, *args, **kwargs) except CoveInputDataError as err: return render(request, 'error.html', context=err.context) return wrapper
[docs]def cove_spreadsheet_conversion_error(func): @functools.wraps(func) def wrapper(request, *args, **kwargs): try: return func(request, *args, **kwargs) except Exception as err: logger.exception(err, extra={ 'request': request, }) raise CoveInputDataError({ 'sub_title': _("Sorry we can't process that data"), 'link': 'index', 'link_text': _('Try Again'), 'msg': _('We think you tried to supply a spreadsheet, but we failed to convert it to JSON.' '\n\nError message: {}'.format(repr(err))) }) return wrapper