Merge lp:~elachuni/piston-mini-client/debug-envvar into lp:piston-mini-client

Proposed by Anthony Lenton
Status: Merged
Merged at revision: 30
Proposed branch: lp:~elachuni/piston-mini-client/debug-envvar
Merge into: lp:piston-mini-client
Diff against target: 87 lines (+37/-1)
4 files modified
doc/envvars.rst (+6/-0)
doc/index.rst (+2/-1)
piston_mini_client/failhandlers.py (+5/-0)
piston_mini_client/tests/test_failhandlers.py (+24/-0)
To merge this branch: bzr merge lp:~elachuni/piston-mini-client/debug-envvar
Reviewer Review Type Date Requested Status
Ricardo Kirkner Approve
Review via email: mp+54365@code.launchpad.net

Description of the change

Overview
========
This branch makes APIErrors aware of the PISTON_MINI_CLIENT_DEBUG environment variable.

Details
=======
If set, APIErrors will print out the full server response by default, to make debugging easier.
Brief docs and tests were also added.

To post a comment you must log in.
31. By Anthony Lenton

Small doc fix and added a test.

Revision history for this message
Ricardo Kirkner (ricardokirkner) wrote :

Very nice work, including the documentation for the change.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'doc/envvars.rst'
2--- doc/envvars.rst 1970-01-01 00:00:00 +0000
3+++ doc/envvars.rst 2011-03-22 16:32:10 +0000
4@@ -0,0 +1,6 @@
5+Environment variables
6+=====================
7+
8+These environment variables affect the behaviour of ``piston_mini_client``:
9+ * ``PISTON_MINI_CLIENT_DEBUG``: If set, all ``APIError`` exceptions will
10+ report the full response from the server when printed, not only the headers.
11
12=== modified file 'doc/index.rst'
13--- doc/index.rst 2011-01-20 03:39:17 +0000
14+++ doc/index.rst 2011-03-22 16:32:10 +0000
15@@ -4,7 +4,7 @@
16 contain the root `toctree` directive.
17
18 Welcome to piston_mini_client's documentation!
19-========================================
20+==============================================
21
22 Contents:
23
24@@ -13,6 +13,7 @@
25
26 quickstart
27 reference
28+ envvars
29
30 Overview
31 ========
32
33=== modified file 'piston_mini_client/failhandlers.py'
34--- piston_mini_client/failhandlers.py 2011-01-20 04:56:36 +0000
35+++ piston_mini_client/failhandlers.py 2011-03-22 16:32:10 +0000
36@@ -17,11 +17,16 @@
37 'MultiExceptionFailHandler',
38 ]
39
40+import os
41+
42 class APIError(Exception):
43 def __init__(self, msg, body=None):
44 self.msg = msg
45 self.body = body
46+ self.debug = os.environ.get('PISTON_MINI_CLIENT_DEBUG', False)
47 def __str__(self):
48+ if self.debug:
49+ return "%s\nReceived body:\n%s" % (self.msg, self.body)
50 return self.msg
51
52
53
54=== modified file 'piston_mini_client/tests/test_failhandlers.py'
55--- piston_mini_client/tests/test_failhandlers.py 2011-01-12 17:21:36 +0000
56+++ piston_mini_client/tests/test_failhandlers.py 2011-03-22 16:32:10 +0000
57@@ -41,6 +41,30 @@
58 return self._get('/plant')
59
60
61+class APIErrorTestCase(TestCase):
62+ def test_default_repr(self):
63+ """Check that usually only msg is printed out"""
64+ err = APIError(msg='foo', body='bar')
65+ err.debug = False
66+ self.assertEqual('foo', str(err))
67+
68+ def test_verbose_repr(self):
69+ """Check that body is also included if in verbose mode"""
70+ err = APIError(msg='foo', body='bar')
71+ err.debug = True
72+ self.assertTrue('bar' in str(err))
73+
74+ @patch('os.environ.get')
75+ def test_debug_gets_set_from_environment(self, mock_get):
76+ """Check that debug is initialized from the environment"""
77+ sentinel = object()
78+ mock_get.return_value = sentinel
79+
80+ err = APIError('foo')
81+
82+ self.assertEqual(err.debug, sentinel)
83+ mock_get.assert_called_with('PISTON_MINI_CLIENT_DEBUG', False)
84+
85 class ExceptionFailHandlerTestCase(TestCase):
86 """As this is the default fail handler, we can skip most tests"""
87 def test_no_status(self):

Subscribers

People subscribed via source and target branches