Merge lp:~maxiberta/launchpad/sshkeyadditionerror-msg-format2 into lp:launchpad

Proposed by Maximiliano Bertacchini on 2018-06-25
Status: Merged
Merged at revision: 18693
Proposed branch: lp:~maxiberta/launchpad/sshkeyadditionerror-msg-format2
Merge into: lp:launchpad
Diff against target: 46 lines (+4/-4)
3 files modified
lib/lp/registry/interfaces/ssh.py (+2/-1)
lib/lp/registry/tests/test_ssh.py (+1/-1)
lib/lp/testing/__init__.py (+1/-2)
To merge this branch: bzr merge lp:~maxiberta/launchpad/sshkeyadditionerror-msg-format2
Reviewer Review Type Date Requested Status
Colin Watson 2018-06-25 Approve on 2018-06-25
Review via email: mp+348473@code.launchpad.net

Commit message

Fix encoding error message when handling SSHKeyAdditionError.

Description of the change

Further encode error message when handling SSHKeyAdditionError. Reverts unicode encoding in assertRaisesWithContent() back to `str(exception)` to make sure it won't crash in lazr's error handling code, as in e.g.:

  UnicodeEncodeError: 'ascii' codec can't encode character u'\xc7' in position 67: ordinal not in range(128)
    Traceback (most recent call last):
  Module zope.app.publication.zopepublication, line 379, in handleException
    body = mapply(view, (), request)
  Module zope.publisher.publish, line 107, in mapply
    return debug_call(obj, args)
   - __traceback_info__: <lazr.restful.error.WebServiceExceptionView object at 0x7fca857f5c10>
  Module zope.publisher.publish, line 113, in debug_call
    return obj(*args)
  Module lazr.restful.error, line 78, in __call__
    result = [str(self.context)]
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc7' in position 67: ordinal not in range(128)

To post a comment you must log in.
Colin Watson (cjwatson) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/lp/registry/interfaces/ssh.py'
--- lib/lp/registry/interfaces/ssh.py 2018-06-20 14:44:48 +0000
+++ lib/lp/registry/interfaces/ssh.py 2018-06-25 15:51:23 +0000
@@ -146,6 +146,7 @@
146 except UnicodeDecodeError:146 except UnicodeDecodeError:
147 # On Python 2, Key.fromString can raise exceptions with147 # On Python 2, Key.fromString can raise exceptions with
148 # non-UTF-8 messages.148 # non-UTF-8 messages.
149 exception_text = bytes(exception).decode('unicode_escape')149 exception_text = bytes(exception).decode(
150 'unicode_escape').encode('unicode_escape')
150 msg = "%s (%s)" % (msg, exception_text)151 msg = "%s (%s)" % (msg, exception_text)
151 super(SSHKeyAdditionError, self).__init__(msg, *args, **kwargs)152 super(SSHKeyAdditionError, self).__init__(msg, *args, **kwargs)
152153
=== modified file 'lib/lp/registry/tests/test_ssh.py'
--- lib/lp/registry/tests/test_ssh.py 2018-06-20 14:44:48 +0000
+++ lib/lp/registry/tests/test_ssh.py 2018-06-25 15:51:23 +0000
@@ -173,7 +173,7 @@
173 self.assertRaisesWithContent(173 self.assertRaisesWithContent(
174 SSHKeyAdditionError,174 SSHKeyAdditionError,
175 "Invalid SSH key data: 'ssh-rsa asdfasdf comment' "175 "Invalid SSH key data: 'ssh-rsa asdfasdf comment' "
176 u"(unknown blob type: \xc7_)",176 "(unknown blob type: \\xc7_)",
177 keyset.new,177 keyset.new,
178 person, 'ssh-rsa asdfasdf comment'178 person, 'ssh-rsa asdfasdf comment'
179 )179 )
180180
=== modified file 'lib/lp/testing/__init__.py'
--- lib/lp/testing/__init__.py 2018-06-25 09:16:49 +0000
+++ lib/lp/testing/__init__.py 2018-06-25 15:51:23 +0000
@@ -95,7 +95,6 @@
95import pytz95import pytz
96import scandir96import scandir
97import simplejson97import simplejson
98import six
99from storm.store import Store98from storm.store import Store
100import subunit99import subunit
101import testtools100import testtools
@@ -652,7 +651,7 @@
652 match what was raised an AssertionError is raised.651 match what was raised an AssertionError is raised.
653 """652 """
654 err = self.assertRaises(exception, func, *args, **kwargs)653 err = self.assertRaises(exception, func, *args, **kwargs)
655 self.assertEqual(exception_content, six.text_type(err))654 self.assertEqual(exception_content, str(err))
656655
657 def assertBetween(self, lower_bound, variable, upper_bound):656 def assertBetween(self, lower_bound, variable, upper_bound):
658 """Assert that 'variable' is strictly between two boundaries."""657 """Assert that 'variable' is strictly between two boundaries."""