python-3.xdjangoemaildjango-formsdjango-oscar

'AssertionError' object has no attribute 'message'


I'm dealing with form in django that once its filled sends an email to the user but im getting the following error:

error image

I have checked in my code and my problem comes from this function:

def send_manually_exception_email(request, e):
  exc_info = sys.exc_info()
  reporter = ExceptionReporter(request, is_email=True, *exc_info)
  subject = e.message.replace('\n', '\\n').replace('\r', '\\r')[:989]
  message = "%s\n\n%s" % (
    '\n'.join(traceback.format_exception(*exc_info)),
    reporter.filter.get_request_repr(request)
  )
  mail.mail_admins(subject, message, fail_silently=True, html_message=reporter.get_traceback_html())

What can I do?


Solution

  • The exception object e does not per se has a message attribute. You can for example retrieve it if there is such attribute, and use the empty string if there is no such attribute with getattr(…) [python-doc]:

    def send_manually_exception_email(request, e):
      exc_info = sys.exc_info()
      reporter = ExceptionReporter(request, is_email=True, *exc_info)
      subject = getattr(e, 'message', '').replace('\n', '\\n').replace('\r', '\\r')[:989]
      message = "%s\n\n%s" % (
        '\n'.join(traceback.format_exception(*exc_info)),
        reporter.filter.get_request_repr(request)
      )
      mail.mail_admins(subject, message, fail_silently=True, html_message=reporter.get_traceback_html())