Merge ~maxiberta/snapstore-client:improve-error-handling-2 into snapstore-client:master

Proposed by Maximiliano Bertacchini
Status: Merged
Approved by: Maximiliano Bertacchini
Approved revision: 9f162ddd97426448ad0c75738ccda802579c0574
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~maxiberta/snapstore-client:improve-error-handling-2
Merge into: snapstore-client:master
Diff against target: 165 lines (+20/-20)
3 files modified
store_admin/exceptions.py (+4/-0)
store_admin/logic/push.py (+9/-13)
store_admin/logic/tests/test_push.py (+7/-7)
Reviewer Review Type Date Requested Status
Wouter van Bommel (community) Approve
Review via email: mp+418569@code.launchpad.net

Commit message

Improve handling of push exceptions

To post a comment you must log in.
Revision history for this message
Wouter van Bommel (woutervb) wrote :

lgtm, minor comment would be that newer code in this project uses f strings

review: Approve
Revision history for this message
Otto Co-Pilot (otto-copilot) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/store_admin/exceptions.py b/store_admin/exceptions.py
2index 0f1f71f..7883a99 100644
3--- a/store_admin/exceptions.py
4+++ b/store_admin/exceptions.py
5@@ -28,6 +28,10 @@ class InvalidStoreURL(ClientError):
6 fmt = "Invalid store url: {message}."
7
8
9+class PushException(ClientError):
10+ fmt = "Error uploading data: {message}."
11+
12+
13 class StoreCommunicationError(ClientError):
14 fmt = "Connection error with the store using: {message}."
15
16diff --git a/store_admin/logic/push.py b/store_admin/logic/push.py
17index 274cf98..b612f14 100644
18--- a/store_admin/logic/push.py
19+++ b/store_admin/logic/push.py
20@@ -25,10 +25,6 @@ logger = logging.getLogger(__name__)
21 USERNAME = "admin"
22
23
24-class pushException(Exception):
25- pass
26-
27-
28 class ChannelMapExistsException(Exception):
29 pass
30
31@@ -63,7 +59,7 @@ def _push_ident(store, password, downloaded_map):
32 auth=requests.auth.HTTPBasicAuth(USERNAME, password),
33 )
34 if response.status_code != 200:
35- raise pushException(
36+ raise exceptions.PushException(
37 "Failure in pushing to snapident: {}".format(response.content)
38 )
39
40@@ -109,7 +105,7 @@ def _push_revs(store, password, downloaded_map):
41 )
42 # 409 / Conflict - already exists
43 if revs_response.status_code not in [201, 409]:
44- raise pushException(
45+ raise exceptions.PushException(
46 "Failure to push revisions to snaprevs: {}".format(
47 revs_response.content
48 )
49@@ -138,7 +134,7 @@ def _push_channelmap(store, password, downloaded_map, force_push=False):
50 auth=requests.auth.HTTPBasicAuth(USERNAME, password),
51 )
52 if existing.status_code != 200:
53- raise pushException("Error retrieving current channelmap")
54+ raise exceptions.PushException("Error retrieving current channelmap")
55 if existing.json().get("channelmaps") and not force_push:
56 raise ChannelMapExistsException("Channel map already exists.")
57
58@@ -173,7 +169,7 @@ def _push_channelmap(store, password, downloaded_map, force_push=False):
59 auth=requests.auth.HTTPBasicAuth(USERNAME, password),
60 )
61 if map_response.status_code != 200:
62- raise pushException("Error pushing channel map")
63+ raise exceptions.PushException("Error pushing channel map")
64
65
66 def _load_assertion_file(assert_path):
67@@ -224,7 +220,7 @@ def _add_assertion_to_service(store, password, list_assertions):
68 auth=requests.auth.HTTPBasicAuth(USERNAME, password),
69 )
70 if response.status_code != 201:
71- raise pushException(
72+ raise exceptions.PushException(
73 "Failed to push: {} - {}".format(response.status_code, response.content)
74 )
75
76@@ -239,8 +235,9 @@ def _push_assertions(store, password, assert_path):
77 # push the assertions to the internal snap-assertion-service
78 try:
79 _add_assertion_to_service(store, password, current_asserts)
80- except pushException:
81- raise pushException("Assertion file: {}".format(assert_path))
82+ except exceptions.PushException:
83+ logger.error("Assertion file: %s", assert_path)
84+ raise
85
86
87 def _push_file_to_nginx_cache(store, password, snap_path, snap_id, revision):
88@@ -254,8 +251,7 @@ def _push_file_to_nginx_cache(store, password, snap_path, snap_id, revision):
89 auth=requests.auth.HTTPBasicAuth(USERNAME, password),
90 )
91 if response.status_code != 200:
92- print(response.status_code)
93- raise pushException("Failed to push file to proxy cache")
94+ raise exceptions.PushException("Failed to push file to proxy cache")
95
96
97 def _push(store, tar_file, password, force_channel_map=False):
98diff --git a/store_admin/logic/tests/test_push.py b/store_admin/logic/tests/test_push.py
99index c694646..432be19 100644
100--- a/store_admin/logic/tests/test_push.py
101+++ b/store_admin/logic/tests/test_push.py
102@@ -5,9 +5,9 @@ from urllib.parse import urljoin
103
104 import responses
105
106+from store_admin.exceptions import PushException
107 from store_admin.logic.push import (
108 ChannelMapExistsException,
109- pushException,
110 _add_assertion_to_service,
111 _split_assertions,
112 _push_channelmap,
113@@ -44,7 +44,7 @@ class Testpush:
114 ident_url = urljoin(self.default_gw_url, "/snaps/update")
115 responses.add("POST", ident_url, status=500)
116
117- pytest.raises(pushException, _push_ident, self.store, "test", self.snap_map)
118+ pytest.raises(PushException, _push_ident, self.store, "test", self.snap_map)
119
120 @responses.activate
121 def test_push_revs(self):
122@@ -64,14 +64,14 @@ class Testpush:
123 revs_url = urljoin(self.default_gw_url, "/revisions/create")
124 responses.add("POST", revs_url, status=302)
125
126- pytest.raises(pushException, _push_revs, self.store, "test", self.snap_map)
127+ pytest.raises(PushException, _push_revs, self.store, "test", self.snap_map)
128
129 @responses.activate
130 def test_push_revs_failed(self):
131 revs_url = urljoin(self.default_gw_url, "/revisions/create")
132 responses.add("POST", revs_url, status=500)
133
134- pytest.raises(pushException, _push_revs, self.store, "test", self.snap_map)
135+ pytest.raises(PushException, _push_revs, self.store, "test", self.snap_map)
136
137 @responses.activate
138 def test_push_map(self):
139@@ -107,7 +107,7 @@ class Testpush:
140 responses.add("POST", update_url, status=500)
141
142 pytest.raises(
143- pushException, _push_channelmap, self.store, "test", self.snap_map
144+ PushException, _push_channelmap, self.store, "test", self.snap_map
145 )
146
147 @responses.activate
148@@ -174,7 +174,7 @@ class Testpush:
149
150 split_assertions = _split_assertions(self.snap_assert)
151 pytest.raises(
152- pushException,
153+ PushException,
154 _add_assertion_to_service,
155 self.store,
156 "test",
157@@ -188,7 +188,7 @@ class Testpush:
158
159 split_assertions = _split_assertions(self.snap_assert)
160 pytest.raises(
161- pushException,
162+ PushException,
163 _add_assertion_to_service,
164 self.store,
165 "test",

Subscribers

People subscribed via source and target branches

to all changes: