Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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?

like image 530
Alex Avatar asked Feb 06 '26 19:02

Alex


2 Answers

Although better than crashing, OP probably doesn't want to generate an email with a blank subject line. You can just use the string representation of the error, eg:

print(e)
print(f'Function x gave error {e}')

In OP's code, it may be good enough to set:

subject = str(e)

Or use this as an alternative if the error does not have a message:

subject = getattr(e, 'message', str(e)).replace('\n', '\\n').replace('\r', '\\r')[:989]
like image 109
InnocentBystander Avatar answered Feb 09 '26 12:02

InnocentBystander


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())
like image 37
Willem Van Onsem Avatar answered Feb 09 '26 11:02

Willem Van Onsem