Repository: fimion/wheniwork-api-py

Issue:
Error handling: make method to throw exceptions
Make this throw errors correctly


Comments:

Patch:
diff --git a/wheniwork/__init__.py b/wheniwork/__init__.py
index 9eef3fd..04c6930 100644
--- a/wheniwork/__init__.py
+++ b/wheniwork/__init__.py
@@ -6,6 +6,16 @@ A Python Wrapper for WhenIWork.com
 import requests
 
 
+def raise_for_status_with_message(resp):
+    try:
+        resp.raise_for_status()
+    except requests.exceptions.HTTPError as error:
+        if resp.text:
+            raise requests.exceptions.HTTPError('{} \nError message: {}'.format(str(error), resp.text))
+        else:
+            raise error
+
+
 class WhenIWork(object):
     """.. py:class: WhenIWork([:param token:=None, :param options:=dict()])
     :param token: The user WhenIWork API token
@@ -134,7 +144,7 @@ class WhenIWork(object):
         head = {'W-Key': key}
         head.update(self.headers)
         resp = requests.post(url, json=params, headers=head)
-        assert resp.ok
+        raise_for_status_with_message(resp)
         self.__api_resp = resp.json()
         data = self.resp
         if 'login' in data and 'token' in data['login']:
@@ -159,7 +169,7 @@ class WhenIWork(object):
                 if headers:
                     head.update(headers)
                 resp = requests.get(url, params, headers=head)
-                assert resp.ok
+                raise_for_status_with_message(resp)
                 self.__api_resp = resp.json()
                 return self.resp
             else:
@@ -184,7 +194,7 @@ class WhenIWork(object):
                 if headers:
                     head.update(headers)
                 resp = requests.post(url, json=params, headers=head)
-                assert resp.ok
+                raise_for_status_with_message(resp)
                 self.__api_resp = resp.json()
                 return self.resp
             else:
@@ -220,7 +230,7 @@ class WhenIWork(object):
                 if headers:
                     head.update(headers)
                 resp = requests.put(url, json=params, headers=head)
-                assert resp.ok
+                raise_for_status_with_message(resp)
                 self.__api_resp = resp.json()
                 return self.resp
             else:
@@ -244,10 +254,10 @@ class WhenIWork(object):
                 if headers:
                     head.update(headers)
                 resp = requests.delete(url, headers=head)
-                assert resp.ok
+                raise_for_status_with_message(resp)
                 self.__api_resp = resp.json()
                 return self.resp
             else:
                 return {'error': 'Token is not set!!'}
         else:
-            return {'error': 'Method is not str!!'}
\ No newline at end of file
+            return {'error': 'Method is not str!!'}


Test patch:
diff --git a/tests/test_init.py b/tests/test_init.py
new file mode 100644
index 0000000..2f16b34
--- /dev/null
+++ b/tests/test_init.py
@@ -0,0 +1,23 @@
+from wheniwork import raise_for_status_with_message
+import pytest
+import requests
+
+
+def test_raise_for_status_with_message_no_error_on_successful_response():
+    successful_response = requests.models.Response()
+    successful_response.status_code = 200
+
+    raise_for_status_with_message(successful_response)
+
+
+def test_raise_for_status_with_message_shows_message_of_error():
+    TEST_ERROR_MESSAGE = 'this should be in error message'
+    EXPECTED_ERROR_MESSAGE = TEST_ERROR_MESSAGE
+    error_500_response = requests.models.Response()
+    error_500_response.status_code = 500
+    error_500_response._content = TEST_ERROR_MESSAGE.encode('utf8')
+
+    with pytest.raises(requests.exceptions.HTTPError) as excinfo:
+        raise_for_status_with_message(error_500_response)
+        assert EXPECTED_ERROR_MESSAGE in str(excinfo.value)
+