TurboGears / backlash

Standalone WebOb port of the Werkzeug Debugger with Python3 support born as a WebError replacement for TurboGears2
MIT License
13 stars 11 forks source link

Email Reporter Fails if Newlines are Present in the Exception #8

Closed href closed 9 years ago

href commented 9 years ago

I discovered that my backlash setup fails to report SQLAlchemy exceptions with the following error:

Error while reporting exception with <backlash.tracing.reporters.mail.EmailReporter object at 0x1070bc198>
Traceback (most recent call last):
  File "/opt/onegov-cloud/lib/python3.4/site-packages/backlash/tracing/reporters/mail.py", line 48, in report
    result = server.sendmail(self.from_address, self.error_email, msg.as_string())
  File "/usr/lib/python3.4/email/message.py", line 158, in as_string
    maxheaderlen=maxheaderlen,
  File "/usr/lib/python3.4/email/generator.py", line 109, in flatten
    self.write(ufrom + self._NL)
  File "/usr/lib/python3.4/email/generator.py", line 189, in _write
    if meth is None:
  File "/usr/lib/python3.4/email/generator.py", line 216, in _write_headers
    for h, v in msg.raw_items():
  File "/usr/lib/python3.4/email/_policybase.py", line 314, in fold
    return self._fold(name, value, sanitize=True)
  File "/usr/lib/python3.4/email/_policybase.py", line 353, in _fold
    maxlinelen=self.max_line_length))
  File "/usr/lib/python3.4/email/header.py", line 393, in encode
    formatter.add_transition()
email.errors.HeaderParseError: header value appears to contain an embedded header: 'sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) update or delete on table "submissions" violates foreign key constraint "submission_files_submission_id_fkey" on table "submission_files"\nDETAIL:  Key (id)=(11111111-2222-3333-4444-555555555555) is still referenced from table "submission_files".\n [SQL: \'DELETE FROM submissions WHERE submissions.state = %(state_1)s AND coalesce(submissions.modified, submissions.created) < %(coalesce_1)s\'] [parameters: {\'coalesce_1\': datetime.datetime(2015, 7, 11, 14, 4, 49, 937645), \'state_1\': \'pending\'}]'0.0.0.0 - - [12/Jul/2015 07:58:10] "GET /onegov_town/govikon/themen/kultur-freizeit HTTP/1.0" 500 59

Replacing all newlines in the subject in EmailReporter.assemble_email seems to fix the problem.

The cause of this error is the following check in the standard library: https://github.com/python/cpython/blob/2.7/Lib/email/header.py#L411

amol- commented 9 years ago

I see, it's actually a new line followed by the colon, so I was unable to replicate it with just newlines.

It's actually caused by \nDETAIL:

Should be fixed by replacing newlines with just spaces in Subject as suggested.

href commented 9 years ago

That's funny, I just asked you some questions at Europython about filedeopt, when you were probably fixing this bug ;)