Skip to content

Commit

Permalink
[#183] Raise exception on invalid title instead
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Jan 14, 2018
1 parent 27998bc commit 19e464d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions mwclient/errors.py
Expand Up @@ -102,3 +102,7 @@ def __init__(self, response_text=None):

def __str__(self):
return self.message


class InvalidPageTitle(MwClientError):
pass
5 changes: 4 additions & 1 deletion mwclient/page.py
Expand Up @@ -36,6 +36,9 @@ def __init__(self, site, name, info=None, extra_properties=None):
info = six.next(six.itervalues(info['query']['pages']))
self._info = info

if 'invalid' in info:
raise mwclient.errors.InvalidPageTitle(info.get('invalidreason'))

self.namespace = info.get('ns', 0)
self.name = info.get('title', u'')
if self.namespace:
Expand All @@ -45,7 +48,7 @@ def __init__(self, site, name, info=None, extra_properties=None):

self.touched = parse_timestamp(info.get('touched'))
self.revision = info.get('lastrevid', 0)
self.exists = 'missing' not in info and 'invalid' not in info
self.exists = 'missing' not in info
self.length = info.get('length')
self.protection = {
i['type']: (i['level'], i['expiry'])
Expand Down
7 changes: 3 additions & 4 deletions test/test_page.py
Expand Up @@ -10,7 +10,7 @@
import mwclient
from mwclient.page import Page
from mwclient.client import Site
from mwclient.errors import APIError, AssertUserFailedError, ProtectedPageError
from mwclient.errors import APIError, AssertUserFailedError, ProtectedPageError, InvalidPageTitle

try:
import json
Expand Down Expand Up @@ -82,9 +82,8 @@ def test_invalid_title(self, mock_site):
}
}
}
page = Page(mock_site, title)

assert page.exists is False
with pytest.raises(InvalidPageTitle):
page = Page(mock_site, title)

@mock.patch('mwclient.client.Site')
def test_pageprops(self, mock_site):
Expand Down

0 comments on commit 19e464d

Please sign in to comment.