diff --git a/bitbucket/bitbucket.py b/bitbucket/bitbucket.py index e7d7b2b..b5989e4 100644 --- a/bitbucket/bitbucket.py +++ b/bitbucket/bitbucket.py @@ -243,12 +243,20 @@ def dispatch(self, method, url, auth=None, params=None, **kwargs): False, 'Unauthorized access, ' 'please check your credentials.') - elif status >= 400 and status < 500: - return (False, 'Service not found.') + elif status == 404: + return (False, dict(message='Service not found', reason=error, code=status)) + elif status == 400: + return (False, dict(message='Bad request sent to server.', reason=error, code=status)) + elif status == 401: + return (False, dict(message='Not enough privileges.', reason=error, code=status)) + elif status == 403: + return (False, dict(message='Not authorized.', reason=error, code=status)) + elif status == 402 or status >= 405: + return (False, dict(message='Request error.', reason=error, code=status)) elif status >= 500 and status < 600: - return (False, 'Server error.') + return (False, dict(message='Server error.', reason=error, code=status)) else: - return (False, error) + return (False, dict(message='Unidentified error.', reason=error, code=status)) def url(self, action, **kwargs): """ Construct and return the URL for a specific API service. """ @@ -270,6 +278,8 @@ def get_user(self, username=None): return (response[0], response[1]['user']) except TypeError: pass + except KeyError: + pass return response def get_tags(self, repo_slug=None): diff --git a/bitbucket/repository.py b/bitbucket/repository.py index b900b9b..7e38bd9 100644 --- a/bitbucket/repository.py +++ b/bitbucket/repository.py @@ -58,6 +58,8 @@ def public(self, username=None): return (response[0], response[1]['repositories']) except TypeError: pass + except KeyError: + pass return response def all(self): diff --git a/bitbucket/tests/public.py b/bitbucket/tests/public.py index 867f7cd..07bdeb6 100755 --- a/bitbucket/tests/public.py +++ b/bitbucket/tests/public.py @@ -114,7 +114,9 @@ def test_get_none_user(self): self.bb.username = None success, result = self.bb.get_user() self.assertFalse(success) - self.assertEqual(result, 'Service not found.') + self.assertEqual(result['message'], 'Service not found') + self.assertEqual(result['reason'], 'NOT FOUND') + self.assertEqual(result['code'], 404) def test_get_public_repos(self): """ Test public_repos on specific user.""" @@ -134,7 +136,9 @@ def test_get_none_public_repos(self): self.bb.username = None success, result = self.bb.repository.public() self.assertFalse(success) - self.assertEqual(result, 'Service not found.') + self.assertEqual(result['message'], 'Service not found') + self.assertEqual(result['reason'], 'NOT FOUND') + self.assertEqual(result['code'], 404) if __name__ == "__main__": unittest.main()