Merge ~kissiel/checkbox-support:snap-utils-improvements into checkbox-support:master

Proposed by Maciej Kisielewski
Status: Merged
Approved by: Maciej Kisielewski
Approved revision: 3971f8c8f8158c128cbf241858cfa252aaee7076
Merged at revision: f67a8962423a458ba933ab494c5751290d77ec6a
Proposed branch: ~kissiel/checkbox-support:snap-utils-improvements
Merge into: checkbox-support:master
Diff against target: 74 lines (+31/-7)
1 file modified
checkbox_support/snap_utils/snapd.py (+31/-7)
Reviewer Review Type Date Requested Status
Jonathan Cave (community) Approve
Maciej Kisielewski Needs Resubmitting
Review via email: mp+394400@code.launchpad.net

Description of the change

make the snap_utils module more informative when it fails

To post a comment you must log in.
Revision history for this message
Sheila Miguez (codersquid) wrote :

Would using raise foo from e help with providing more information? The cause would be set and it prints out more information about it when printing the stack trace.

Revision history for this message
Jonathan Cave (jocave) wrote :

One nitpick below, otherwise good with me

review: Approve
Revision history for this message
Maciej Kisielewski (kissiel) wrote :

> Would using raise foo from e help with providing more information? The cause
> would be set and it prints out more information about it when printing the
> stack trace.

Yeah! Fixed and squashed.

Revision history for this message
Maciej Kisielewski (kissiel) wrote :

> One nitpick below, otherwise good with me

No diff comment :(

Revision history for this message
Maciej Kisielewski (kissiel) wrote :

Changed @staticmethod to @classmethod

review: Needs Resubmitting
Revision history for this message
Jonathan Cave (jocave) wrote :

LGTM - fixed my undocumented nitpick :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/checkbox_support/snap_utils/snapd.py b/checkbox_support/snap_utils/snapd.py
index 8cb5c7c..be80120 100644
--- a/checkbox_support/snap_utils/snapd.py
+++ b/checkbox_support/snap_utils/snapd.py
@@ -7,6 +7,8 @@
7import json7import json
8import time8import time
99
10from requests.exceptions import HTTPError
11
10import requests_unixsocket12import requests_unixsocket
1113
1214
@@ -16,6 +18,17 @@ class AsyncException(Exception):
16 self.message = message18 self.message = message
17 self.abort_message = abort_message19 self.abort_message = abort_message
1820
21class SnapdRequestError(Exception):
22 def __init__(self, message, kind):
23 self.message = message
24 self.kind = kind
25
26 @classmethod
27 def from_http_error(cls, http_error):
28 response = http_error.response.json()['result']
29 return cls(
30 response['message'], response.get('kind', ''))
31
1932
20class Snapd():33class Snapd():
2134
@@ -40,16 +53,22 @@ class Snapd():
4053
41 def _get(self, path, params=None, decode=True):54 def _get(self, path, params=None, decode=True):
42 r = self._session.get(self._url + path, params=params)55 r = self._session.get(self._url + path, params=params)
43 r.raise_for_status()56 try:
44 if decode:57 r.raise_for_status()
45 return r.json()58 if decode:
59 return r.json()
60 except HTTPError as exc:
61 raise SnapdRequestError.from_http_error(exc) from exc
46 return r62 return r
4763
48 def _post(self, path, data=None, decode=True):64 def _post(self, path, data=None, decode=True):
49 r = self._session.post(self._url + path, data=data)65 r = self._session.post(self._url + path, data=data)
50 r.raise_for_status()66 try:
51 if decode:67 r.raise_for_status()
52 return r.json()68 if decode:
69 return r.json()
70 except HTTPError as exc:
71 raise SnapdRequestError.from_http_error(exc) from exc
53 return r72 return r
5473
55 def _put(self, path, data=None, decode=True):74 def _put(self, path, data=None, decode=True):
@@ -83,7 +102,12 @@ class Snapd():
83 path = self._snaps102 path = self._snaps
84 if snap is not None:103 if snap is not None:
85 path += '/' + snap104 path += '/' + snap
86 return self._get(path)['result']105 try:
106 return self._get(path)['result']
107 except SnapdRequestError as exc:
108 if exc.kind == 'snap-not-found':
109 return None
110 raise
87111
88 def install(self, snap, channel='stable', revision=None):112 def install(self, snap, channel='stable', revision=None):
89 path = self._snaps + '/' + snap113 path = self._snaps + '/' + snap

Subscribers

People subscribed via source and target branches