Skip to content

Commit bf0ea0d

Browse files
committed
Merge pull request #49 from Kinto/46/handle-403-for-create-collection
Issue a better error message for create_collection.
2 parents ec60537 + e87e989 commit bf0ea0d

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

kinto_client/__init__.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,17 @@ def create_collection(self, collection=None, bucket=None,
220220
safe=safe)
221221
headers = DO_NOT_OVERWRITE if safe else None
222222
endpoint = self._get_endpoint('collection', bucket, collection)
223-
resp, _ = self.session.request('put', endpoint, data=data,
224-
permissions=permissions,
225-
headers=headers)
223+
try:
224+
resp, _ = self.session.request('put', endpoint, data=data,
225+
permissions=permissions,
226+
headers=headers)
227+
except KintoException as e:
228+
if e.response.status_code == 403:
229+
msg = ("Unauthorized. Please check that the bucket exists and "
230+
"that you have the permission to create or write on "
231+
"this collection.")
232+
e = KintoException(msg, e)
233+
raise e
226234
return resp
227235

228236
def update_collection(self, data=None, collection=None, bucket=None,
@@ -285,9 +293,18 @@ def create_record(self, data, id=None, collection=None, permissions=None,
285293
headers = DO_NOT_OVERWRITE if safe else None
286294

287295
endpoint = self._get_endpoint('record', bucket, collection, id)
288-
resp, _ = self.session.request('put', endpoint, data=data,
289-
permissions=permissions,
290-
headers=headers)
296+
try:
297+
resp, _ = self.session.request('put', endpoint, data=data,
298+
permissions=permissions,
299+
headers=headers)
300+
except KintoException as e:
301+
if e.response.status_code == 403:
302+
msg = ("Unauthorized. Please check that the collection exists "
303+
"and that you have the permission to create or write on "
304+
"this collection record.")
305+
e = KintoException(msg, e)
306+
raise e
307+
291308
return resp
292309

293310
def update_record(self, data, id=None, collection=None, permissions=None,

kinto_client/exceptions.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
class KintoException(Exception):
2-
pass
3-
4-
5-
class BucketNotFound(KintoException):
62
request = None
73
response = None
84

95
def __init__(self, message=None, exception=None):
10-
super(BucketNotFound, self).__init__(self, message)
6+
super(KintoException, self).__init__(self, message)
117
self.message = message
128
if exception is not None:
139
self.request = exception.request
1410
self.response = exception.response
11+
else:
12+
self.request = None
13+
self.response = None
14+
15+
16+
class BucketNotFound(KintoException):
17+
pass

kinto_client/tests/test_client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,17 @@ def test_get_or_create_raise_in_other_cases(self):
406406
collection="coll",
407407
if_not_exists=True)
408408

409+
def test_create_collection_raises_a_special_error_on_403(self):
410+
self.session.request.side_effect = get_http_error(status=403)
411+
with self.assertRaises(KintoException) as e:
412+
self.client.create_collection(
413+
bucket="buck",
414+
collection="coll")
415+
expected_msg = ("Unauthorized. Please check that the bucket exists "
416+
"and that you have the permission to create or write "
417+
"on this collection.")
418+
assert e.exception.message == expected_msg
419+
409420

410421
class RecordTest(unittest.TestCase):
411422
def setUp(self):
@@ -674,3 +685,15 @@ def test_get_or_create_raise_in_other_cases(self):
674685
collection="coll",
675686
data={'foo': 'bar'},
676687
if_not_exists=True)
688+
689+
def test_create_record_raises_a_special_error_on_403(self):
690+
self.session.request.side_effect = get_http_error(status=403)
691+
with self.assertRaises(KintoException) as e:
692+
self.client.create_record(
693+
bucket="buck",
694+
collection="coll",
695+
data={'foo': 'bar'})
696+
expected_msg = ("Unauthorized. Please check that the collection exists"
697+
" and that you have the permission to create or write "
698+
"on this collection record.")
699+
assert e.exception.message == expected_msg

kinto_client/tests/test_endpoints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ def test_missing_arguments_raise_an_error(self):
4747
with self.assertRaises(KintoException) as context:
4848
self.endpoints.get('record', bucket='buck', collection='coll')
4949
msg = "Cannot get record endpoint, id is missing"
50-
assert text_type(context.exception) == msg
50+
assert msg in text_type(context.exception)
5151

5252
def test_null_arguments_raise_an_error(self):
5353
# Include a null record id; it should raise an error.
5454
with self.assertRaises(KintoException) as context:
5555
self.endpoints.get('record', bucket='buck', collection='coll',
5656
id=None)
5757
msg = "Cannot get record endpoint, id is missing"
58-
assert text_type(context.exception) == msg
58+
assert msg in text_type(context.exception)
5959

6060
def test_arguments_are_slugified(self):
6161
assert self.endpoints.get('bucket', bucket='My Bucket') ==\

0 commit comments

Comments
 (0)