Merge lp:~alecu/ubuntu-sso-client/extend-errdicts into lp:ubuntu-sso-client

Proposed by Alejandro J. Cura
Status: Merged
Approved by: Vincenzo Di Somma
Approved revision: 588
Merged at revision: 587
Proposed branch: lp:~alecu/ubuntu-sso-client/extend-errdicts
Merge into: lp:ubuntu-sso-client
Diff against target: 98 lines (+66/-4)
2 files modified
ubuntu_sso/main.py (+12/-4)
ubuntu_sso/tests/test_main.py (+54/-0)
To merge this branch: bzr merge lp:~alecu/ubuntu-sso-client/extend-errdicts
Reviewer Review Type Date Requested Status
Vincenzo Di Somma (community) Approve
Natalia Bidart (community) Approve
Review via email: mp+33657@code.launchpad.net

Commit message

Turn exceptions into a dictionary able to go thru DBus (LP: #624018)

Description of the change

The lower DBus layer should pass the error messages sent by the webservice as DBus dictionaries, so the GUI can show the corresponding message next to each input field that failed the checks.

To post a comment you must log in.
588. By Alejandro J. Cura

the arguments for the exception are sent thru dbus as a value of the dict

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Awesome! Remember to put a commit message with the LP bug #. Thanks!

review: Approve
Revision history for this message
Vincenzo Di Somma (vds) wrote :

Looks good.

review: Approve
Revision history for this message
Vincenzo Di Somma (vds) wrote :

Looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/main.py'
2--- ubuntu_sso/main.py 2010-08-24 14:13:53 +0000
3+++ ubuntu_sso/main.py 2010-08-25 16:13:44 +0000
4@@ -289,10 +289,18 @@
5
6 def except_to_errdict(e):
7 """Turn an exception into a dictionary to return thru DBus."""
8- return {
9- "errtype": type(e).__name__,
10- "message": "\n".join(str(arg) for arg in e.args),
11+ result = {
12+ "errtype": e.__class__.__name__,
13+ "errargs": repr(e.args),
14 }
15+ if len(e.args) == 0:
16+ result["message"] = e.__class__.__doc__
17+ elif isinstance(e.args[0], dict):
18+ result.update(e.args[0])
19+ elif isinstance(e.args[0], basestring):
20+ result["message"] = e.args[0]
21+
22+ return result
23
24
25 def blocking(f, app_name, result_cb, error_cb):
26@@ -302,7 +310,7 @@
27 try:
28 result_cb(app_name, f())
29 except Exception, e:
30- msg = "Exception while running DBus blocking code in a thread."""
31+ msg = "Exception while running DBus blocking code in a thread:"
32 logger.exception(msg)
33 error_cb(app_name, except_to_errdict(e))
34 threading.Thread(target=_in_thread).start()
35
36=== modified file 'ubuntu_sso/tests/test_main.py'
37--- ubuntu_sso/tests/test_main.py 2010-08-24 13:46:12 +0000
38+++ ubuntu_sso/tests/test_main.py 2010-08-25 16:13:44 +0000
39@@ -735,6 +735,60 @@
40 return d
41
42
43+class TestExceptToErrdictException(Exception):
44+ """A dummy exception for the following testcase."""
45+
46+
47+class ExceptToErrdictTestCase(TestCase):
48+ """Tests for the except_to_errdict function."""
49+
50+ def test_first_arg_is_dict(self):
51+ """If the first arg is a dict, use it as the base dict."""
52+ sample_dict = {
53+ "errorcode1": "error message 1",
54+ "errorcode2": "error message 2",
55+ "errorcode3": "error message 3",
56+ }
57+ e = TestExceptToErrdictException(sample_dict)
58+ result = except_to_errdict(e)
59+
60+ self.assertEqual(result["errtype"], e.__class__.__name__)
61+ for k in sample_dict.keys():
62+ self.assertIn(k, result)
63+ self.assertEqual(result[k], sample_dict[k])
64+
65+ def test_first_arg_is_str(self):
66+ """If the first arg is a str, use it as the message."""
67+ sample_string = "a sample string"
68+ e = TestExceptToErrdictException(sample_string)
69+ result = except_to_errdict(e)
70+ self.assertEqual(result["errtype"], e.__class__.__name__)
71+ self.assertEqual(result["message"], sample_string)
72+
73+ def test_first_arg_is_unicode(self):
74+ """If the first arg is a unicode, use it as the message."""
75+ sample_string = u"a sample string"
76+ e = TestExceptToErrdictException(sample_string)
77+ result = except_to_errdict(e)
78+ self.assertEqual(result["errtype"], e.__class__.__name__)
79+ self.assertEqual(result["message"], sample_string)
80+
81+ def test_no_args_at_all(self):
82+ """If there are no args, use the class docstring."""
83+ e = TestExceptToErrdictException()
84+ result = except_to_errdict(e)
85+ self.assertEqual(result["errtype"], e.__class__.__name__)
86+ self.assertEqual(result["message"], e.__class__.__doc__)
87+
88+ def test_some_other_thing_as_first_arg(self):
89+ """If first arg is not basestring nor dict, then repr all args."""
90+ sample_args = (None, u"unicode2\ufffd", "errorcode3")
91+ e = TestExceptToErrdictException(*sample_args)
92+ result = except_to_errdict(e)
93+ self.assertEqual(result["errtype"], e.__class__.__name__)
94+ self.assertEqual(result["errargs"], repr(sample_args))
95+
96+
97 class KeyringCredentialsTestCase(MockerTestCase):
98 """Checks the functions that access the keyring."""
99

Subscribers

People subscribed via source and target branches