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
1diff --git a/checkbox_support/snap_utils/snapd.py b/checkbox_support/snap_utils/snapd.py
2index 8cb5c7c..be80120 100644
3--- a/checkbox_support/snap_utils/snapd.py
4+++ b/checkbox_support/snap_utils/snapd.py
5@@ -7,6 +7,8 @@
6 import json
7 import time
8
9+from requests.exceptions import HTTPError
10+
11 import requests_unixsocket
12
13
14@@ -16,6 +18,17 @@ class AsyncException(Exception):
15 self.message = message
16 self.abort_message = abort_message
17
18+class SnapdRequestError(Exception):
19+ def __init__(self, message, kind):
20+ self.message = message
21+ self.kind = kind
22+
23+ @classmethod
24+ def from_http_error(cls, http_error):
25+ response = http_error.response.json()['result']
26+ return cls(
27+ response['message'], response.get('kind', ''))
28+
29
30 class Snapd():
31
32@@ -40,16 +53,22 @@ class Snapd():
33
34 def _get(self, path, params=None, decode=True):
35 r = self._session.get(self._url + path, params=params)
36- r.raise_for_status()
37- if decode:
38- return r.json()
39+ try:
40+ r.raise_for_status()
41+ if decode:
42+ return r.json()
43+ except HTTPError as exc:
44+ raise SnapdRequestError.from_http_error(exc) from exc
45 return r
46
47 def _post(self, path, data=None, decode=True):
48 r = self._session.post(self._url + path, data=data)
49- r.raise_for_status()
50- if decode:
51- return r.json()
52+ try:
53+ r.raise_for_status()
54+ if decode:
55+ return r.json()
56+ except HTTPError as exc:
57+ raise SnapdRequestError.from_http_error(exc) from exc
58 return r
59
60 def _put(self, path, data=None, decode=True):
61@@ -83,7 +102,12 @@ class Snapd():
62 path = self._snaps
63 if snap is not None:
64 path += '/' + snap
65- return self._get(path)['result']
66+ try:
67+ return self._get(path)['result']
68+ except SnapdRequestError as exc:
69+ if exc.kind == 'snap-not-found':
70+ return None
71+ raise
72
73 def install(self, snap, channel='stable', revision=None):
74 path = self._snaps + '/' + snap

Subscribers

People subscribed via source and target branches