Skip to content

Commit

Permalink
Add error codes returned from Bugzilla to errror messages
Browse files Browse the repository at this point in the history
This means that in addition to the error message returned to the user, they will
now also get the error code that they can then check against the Bugzilla
documentation.
  • Loading branch information
AutomatedTester committed Feb 1, 2016
1 parent 5573ef8 commit cd04c3e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 24 deletions.
4 changes: 2 additions & 2 deletions bugsy/bugsy.py
Expand Up @@ -184,7 +184,7 @@ def _handle_errors(self, response):
and result.get('error', False) is True):

if "API key" in result['message'] or "username or password" in result['message']:
raise LoginException(result['message'])
raise LoginException(result['message'], result.get("code"))
else:
raise BugsyException(result["message"])
raise BugsyException(result["message"], result.get("code"))
return result
6 changes: 4 additions & 2 deletions bugsy/errors.py
Expand Up @@ -3,11 +3,13 @@ class BugsyException(Exception):
If while interacting with Bugzilla and we try do something that is not
supported this error will be raised.
"""
def __init__(self, msg):
def __init__(self, msg, error_code=None):
self.msg = msg
self.code = error_code

def __str__(self):
return "Message: %s" % self.msg
return "Message: {message} Code: {code}".format(message=self.msg,
code=self.code)


class LoginException(BugsyException):
Expand Down
2 changes: 1 addition & 1 deletion bugsy/search.py
Expand Up @@ -180,6 +180,6 @@ def search(self):
try:
results = self._bugsy.request('bug', params=params)
except Exception as e:
raise SearchException(e.msg)
raise SearchException(e.msg, e.code)

return [Bug(self._bugsy, **bug) for bug in results['bugs']]
12 changes: 6 additions & 6 deletions tests/test_bugs.py
Expand Up @@ -72,7 +72,7 @@ def test_we_cant_set_status_unless_there_is_a_bug_id():
try:
bug.status = 'RESOLVED'
except BugException as e:
assert str(e) == "Message: Can not set status unless there is a bug id. Please call Update() before setting"
assert str(e) == "Message: Can not set status unless there is a bug id. Please call Update() before setting Code: None"

def test_we_can_get_OS_set_from_default():
bug = Bug()
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_we_throw_an_error_for_invalid_status_types():
bug.status = "foo"
assert 1 == 0, "Should have thrown an error about invalid type"
except BugException as e:
assert str(e) == "Message: Invalid status type was used"
assert str(e) == "Message: Invalid status type was used Code: None"

def test_we_can_get_the_resolution():
bug = Bug(**example_return['bugs'][0])
Expand All @@ -126,7 +126,7 @@ def test_we_cant_set_the_resolution_when_not_valid():
bug.resolution = 'FOO'
assert 1==0, "Should thrown an error"
except BugException as e:
assert str(e) == "Message: Invalid resolution type was used"
assert str(e) == "Message: Invalid resolution type was used Code: None"

def test_we_can_pass_in_dict_and_get_a_bug():
bug = Bug(**example_return['bugs'][0])
Expand Down Expand Up @@ -174,7 +174,7 @@ def test_we_cant_update_unless_we_have_a_bug_id():
try:
bug.update()
except BugException as e:
assert str(e) == "Message: Unable to update bug that isn't in Bugzilla"
assert str(e) == "Message: Unable to update bug that isn't in Bugzilla Code: None"

@responses.activate
def test_we_can_update_a_bug_with_login_token():
Expand Down Expand Up @@ -293,7 +293,7 @@ def test_we_raise_an_exception_when_getting_comments_and_bugzilla_errors():
comments = bug.get_comments()
assert False, "Should have raised an BugException for the bug not existing"
except BugsyException as e:
assert str(e) == "Message: The requested method 'Bug.comments' was not found."
assert str(e) == "Message: The requested method 'Bug.comments' was not found. Code: 67399"

@responses.activate
def test_we_raise_an_exception_if_commenting_on_a_bug_that_returns_an_error():
Expand All @@ -320,7 +320,7 @@ def test_we_raise_an_exception_if_commenting_on_a_bug_that_returns_an_error():
bug.add_comment("I like sausages")
assert False, "Should have raised an BugException for the bug not existing"
except BugsyException as e:
assert str(e) == "Message: Bug 1017315 does not exist."
assert str(e) == "Message: Bug 1017315 does not exist. Code: 101"

assert len(responses.calls) == 3

Expand Down
14 changes: 7 additions & 7 deletions tests/test_bugsy.py
Expand Up @@ -29,7 +29,7 @@ def test_we_cant_post_without_a_username_or_password():
bugzilla.put("foo")
assert 1 == 0, "Should have thrown when calling put"
except BugsyException as e:
assert str(e) == "Message: Unfortunately you can't put bugs in Bugzilla without credentials"
assert str(e) == "Message: Unfortunately you can't put bugs in Bugzilla without credentials Code: None"

@responses.activate
def test_we_get_a_login_exception_when_details_are_wrong():
Expand All @@ -40,7 +40,7 @@ def test_we_get_a_login_exception_when_details_are_wrong():
Bugsy("foo", "bar")
assert 1 == 0, "Should have thrown an error"
except LoginException as e:
assert str(e) == "Message: The username or password you entered is not valid."
assert str(e) == "Message: The username or password you entered is not valid. Code: None"

@responses.activate
def test_bad_api_key():
Expand All @@ -53,7 +53,7 @@ def test_bad_api_key():
Bugsy(username='foo', api_key='badkey')
assert False, 'Should have thrown'
except LoginException as e:
assert str(e) == 'Message: The API key you specified is invalid. Please check that you typed it correctly.'
assert str(e) == 'Message: The API key you specified is invalid. Please check that you typed it correctly. Code: 306'

@responses.activate
def test_validate_api_key():
Expand All @@ -73,7 +73,7 @@ def test_we_cant_post_without_passing_a_bug_object():
bugzilla.put("foo")
assert 1 == 0, "Should have thrown an error about type when calling put"
except BugsyException as e:
assert str(e) == "Message: Please pass in a Bug object when posting to Bugzilla"
assert str(e) == "Message: Please pass in a Bug object when posting to Bugzilla Code: None"

@responses.activate
def test_we_can_get_a_bug():
Expand Down Expand Up @@ -163,7 +163,7 @@ def test_we_handle_errors_from_bugzilla_when_posting():
bugzilla.put(bug)
assert 1 == 0, "Put should have raised an error"
except BugsyException as e:
assert str(e) == "Message: You must select/enter a component."
assert str(e) == "Message: You must select/enter a component. Code: 50"

@responses.activate
def test_we_handle_errors_from_bugzilla_when_updating_a_bug():
Expand All @@ -181,7 +181,7 @@ def test_we_handle_errors_from_bugzilla_when_updating_a_bug():
try:
bugzilla.put(bug)
except BugsyException as e:
assert str(e) == "Message: You must select/enter a component."
assert str(e) == "Message: You must select/enter a component. Code: 50"

@responses.activate
def test_we_can_set_the_user_agent_to_bugsy():
Expand All @@ -207,6 +207,6 @@ def test_we_can_handle_errors_when_retrieving_bugs():
bug = bugzilla.get(111111111)
assert False, "A BugsyException should have been thrown"
except BugsyException as e:
assert str(e) == "Message: Bug 111111111111 does not exist."
assert str(e) == "Message: Bug 111111111111 does not exist. Code: 101"
except Exception as e:
assert False, "Wrong type of exception was thrown"
10 changes: 5 additions & 5 deletions tests/test_errors.py
Expand Up @@ -15,7 +15,7 @@ def test_an_exception_is_raised_when_we_hit_an_error():
bugzilla = Bugsy()
with pytest.raises(BugsyException) as e:
bugzilla.get(1017315)
assert str(e.value) == "Message: We received a 500 error with the following: It's all broken"
assert str(e.value) == "Message: We received a 500 error with the following: It's all broken Code: None"


@responses.activate
Expand All @@ -26,7 +26,7 @@ def test_bugsyexception_raised_for_http_502_when_retrieving_bugs():
bugzilla = Bugsy()
with pytest.raises(BugsyException) as e:
r = bugzilla.get(123456)
assert str(e.value) == "Message: We received a 502 error with the following: Bad Gateway"
assert str(e.value) == "Message: We received a 502 error with the following: Bad Gateway Code: None"


@responses.activate
Expand All @@ -35,7 +35,7 @@ def test_bugsyexception_raised_for_http_503_when_verifying_api_key():
body='Service Unavailable', status=503, content_type='text/html')
with pytest.raises(BugsyException) as e:
Bugsy(username='foo', api_key='goodkey')
assert str(e.value) == "Message: We received a 503 error with the following: Service Unavailable"
assert str(e.value) == "Message: We received a 503 error with the following: Service Unavailable Code: None"


@responses.activate
Expand All @@ -54,7 +54,7 @@ def test_bugsyexception_raised_for_http_500_when_commenting_on_a_bug():
content_type='text/html', match_querystring=True)
with pytest.raises(BugsyException) as e:
bug.add_comment("I like sausages")
assert str(e.value) == "Message: We received a 500 error with the following: Internal Server Error"
assert str(e.value) == "Message: We received a 500 error with the following: Internal Server Error Code: None"


@responses.activate
Expand All @@ -80,4 +80,4 @@ def test_bugsyexception_raised_for_http_500_when_adding_tags_to_bug_comments():
content_type='text/html', match_querystring=True)
with pytest.raises(BugsyException) as e:
comments[0].add_tags("foo")
assert str(e.value) == "Message: We received a 500 error with the following: Internal Server Error"
assert str(e.value) == "Message: We received a 500 error with the following: Internal Server Error Code: None"
2 changes: 1 addition & 1 deletion tests/test_search.py
Expand Up @@ -434,4 +434,4 @@ def test_we_can_handle_errors_coming_back_from_search():
.timeframe('2014-12-01', '2014-12-05')\
.search()
except SearchException as e:
assert str(e) == "Message: Can't use [Bug Creation] as a field name."
assert str(e) == "Message: Can't use [Bug Creation] as a field name. Code: 108"

0 comments on commit cd04c3e

Please sign in to comment.