diff --git a/syncano/exceptions.py b/syncano/exceptions.py index bcd3f2a..1e2e7d8 100644 --- a/syncano/exceptions.py +++ b/syncano/exceptions.py @@ -75,3 +75,7 @@ class SyncanoDoesNotExist(SyncanoException): class RevisionMismatchException(SyncanoRequestError): """Revision do not match with expected one""" + + +class UserNotFound(SyncanoRequestError): + """Special error to handle user not found case.""" diff --git a/syncano/models/accounts.py b/syncano/models/accounts.py index 8623c49..7737f48 100644 --- a/syncano/models/accounts.py +++ b/syncano/models/accounts.py @@ -1,4 +1,4 @@ -from syncano.exceptions import SyncanoValueError +from syncano.exceptions import SyncanoRequestError, SyncanoValueError, UserNotFound from . import fields from .base import Model @@ -214,7 +214,14 @@ def _group_users_method(self, user_id=None, method='GET'): if user_id is not None: endpoint += '{}/'.format(user_id) connection = self._get_connection() - return connection.request(method, endpoint) + try: + response = connection.request(method, endpoint) + except SyncanoRequestError as e: + if e.status_code == 404: + raise UserNotFound(e.status_code, 'User not found.') + raise + + return response def list_users(self): return self._group_users_method() diff --git a/tests/integration_test_user_profile.py b/tests/integration_test_user.py similarity index 76% rename from tests/integration_test_user_profile.py rename to tests/integration_test_user.py index 37a8def..457ee0e 100644 --- a/tests/integration_test_user_profile.py +++ b/tests/integration_test_user.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from syncano.exceptions import UserNotFound from syncano.models import User from tests.integration_test import InstanceMixin, IntegrationTest @@ -39,3 +40,18 @@ def test_profile_change_schema(self): self.user.profile.save() user = User.please.get(id=self.user.id) self.assertEqual(user.profile.profile_pic, self.SAMPLE_PROFILE_PIC) + + +class UserTest(InstanceMixin, IntegrationTest): + + @classmethod + def setUpClass(cls): + super(UserTest, cls).setUpClass() + + cls.group = cls.instance.groups.create( + label='testgroup' + ) + + def test_if_custom_error_is_raised_on_user_group(self): + with self.assertRaises(UserNotFound): + self.group.user_details(user_id=221)