Browse files

Add exceptions for error HTTP statuses

  • Loading branch information...
1 parent 3397b22 commit 026e1d8f072f5da5f95fe085cd697f34065e0118 @txels committed Apr 30, 2012
Showing with 25 additions and 5 deletions.
  1. +16 −3 autojenkins/jobs.py
  2. +9 −2 autojenkins/tests/test_unit_jobs.py
View
19 autojenkins/jobs.py
@@ -18,16 +18,29 @@
DISABLE = '{0}/job/{1}/disable'
-class HttpNotFoundError(Exception):
+class HttpStatusError(Exception):
pass
+class HttpNotFoundError(HttpStatusError):
+ pass
+
+
+HTTP_ERROR_MAP = {
+ 404: HttpNotFoundError,
+}
+
+
def _validate(response):
"""
Verify the status code of the response and raise exception on 404
"""
- if response.status_code == 404:
- raise HttpNotFoundError()
+ message = 'HTTP Status: {0}'.format(response.status_code)
+ if response.status_code >= 400:
+ print(message)
+ exception_cls = HTTP_ERROR_MAP.get(response.status_code,
+ HttpStatusError)
+ raise exception_cls(message)
return response
View
11 autojenkins/tests/test_unit_jobs.py
@@ -4,7 +4,7 @@
from ddt import ddt, data
from mock import Mock, patch
-from autojenkins.jobs import Jenkins, HttpNotFoundError
+from autojenkins.jobs import Jenkins, HttpNotFoundError, HttpStatusError
fixture_path = path.dirname(__file__)
@@ -123,9 +123,16 @@ def test_post_methods_with_jobname(self, case, requests, Template):
auth=None)
self.assertEqual(302, response.status_code)
- def test_404_raises_job_not_found(self, requests, Template):
+ def test_404_raises_http_not_found(self, requests, Template):
http404_response = Mock()
http404_response.status_code = 404
requests.get.return_value = http404_response
with self.assertRaises(HttpNotFoundError):
self.jenkins.last_build_info('job123')
+
+ def test_500_raises_http_error(self, requests, Template):
+ http500_response = Mock()
+ http500_response.status_code = 500
+ requests.get.return_value = http500_response
+ with self.assertRaises(HttpStatusError):
+ self.jenkins.last_build_info('job123')

0 comments on commit 026e1d8

Please sign in to comment.