Skip to content

Commit

Permalink
Add status_code to TravisError (#38)
Browse files Browse the repository at this point in the history
I wanted to access it to filter 404's but the constructor was removing it from _contents so it wasn't accessible. This makes it a permanent attribute of the exception.
  • Loading branch information
adamchainz authored and menegazzo committed Oct 24, 2016
1 parent 3cb3be8 commit d3ce7f3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 8 additions & 0 deletions travispy/_tests/test_errors.py
@@ -0,0 +1,8 @@
from travispy.errors import TravisError


class Tests:

def test_status_code(self):
error = TravisError({'status_code': 404, 'error': 'Not Found'})
assert error.status_code == 404
5 changes: 2 additions & 3 deletions travispy/errors.py
Expand Up @@ -8,11 +8,10 @@ class TravisError(Exception):

def __init__(self, contents):
self._contents = contents
self.status_code = contents['status_code']
Exception.__init__(self, self.message())

def message(self):
status_code = self._contents.pop('status_code')

# Trying to get error message from "error/message" key.
message = self._contents.get('error')
if isinstance(message, dict):
Expand All @@ -22,4 +21,4 @@ def message(self):
if message is None:
message = self._contents.get('file')

return '[%d] %s' % (status_code, message or 'Unknown error')
return '[%d] %s' % (self.status_code, message or 'Unknown error')

0 comments on commit d3ce7f3

Please sign in to comment.