Skip to content

Commit e236674

Browse files
LeMystDennis Priskorn
andauthored
Add NonExistentEntityError exception (#331)
* Add NonExistentEntityError exception Co-Authored-By: Dennis Priskorn <dennis@priskorn.se> * Add missing Exception Co-authored-by: Dennis Priskorn <dennis@priskorn.se>
1 parent b3c16ca commit e236674

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

test/test_entity_item.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from wikibaseintegrator import WikibaseIntegrator
66
from wikibaseintegrator.datatypes import BaseDataType, Item
77
from wikibaseintegrator.wbi_config import config as wbi_config
8+
from wikibaseintegrator.wbi_exceptions import NonExistentEntityError
89

910
wbi_config['USER_AGENT'] = 'WikibaseIntegrator-pytest/1.0 (test_entity_item.py)'
1011

@@ -33,6 +34,10 @@ def test_get(self):
3334
with self.assertRaises(ValueError):
3435
wbi.item.get(-1)
3536

37+
# Test with negative id
38+
with self.assertRaises(NonExistentEntityError):
39+
wbi.item.get("Q99999999999999")
40+
3641
def test_get_json(self):
3742
assert wbi.item.get('Q582').get_json()['labels']['fr']['value'] == 'Villeurbanne'
3843

wikibaseintegrator/entities/baseentity.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from wikibaseintegrator.datatypes import BaseDataType
1111
from wikibaseintegrator.models.claims import Claim, Claims
1212
from wikibaseintegrator.wbi_enums import ActionIfExists
13-
from wikibaseintegrator.wbi_exceptions import MWApiError, NonUniqueLabelDescriptionPairError
13+
from wikibaseintegrator.wbi_exceptions import MWApiError, NonExistentEntityError, NonUniqueLabelDescriptionPairError
1414
from wikibaseintegrator.wbi_helpers import delete_page, mediawiki_api_call_helper
1515
from wikibaseintegrator.wbi_login import _Login
1616

@@ -123,8 +123,14 @@ def get_json(self) -> Dict[str, Union[str, Dict[str, List]]]:
123123
return json_data
124124

125125
def from_json(self, json_data: Dict[str, Any]) -> BaseEntity:
126-
if 'missing' in json_data:
127-
raise ValueError('Entity is nonexistent')
126+
"""
127+
Import a dictionary into BaseEntity attributes.
128+
129+
:param json_data: A specific dictionary from MediaWiki API
130+
:return:
131+
"""
132+
if 'missing' in json_data: # TODO: 1.35 compatibility
133+
raise NonExistentEntityError('The MW API returned that the entity was missing.')
128134

129135
if 'title' in json_data: # TODO: 1.35 compatibility
130136
self.title = str(json_data['title'])

wikibaseintegrator/wbi_exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ class CorePropIntegrityException(Exception):
6060

6161
class MergeError(Exception):
6262
pass
63+
64+
65+
class NonExistentEntityError(Exception):
66+
pass

wikibaseintegrator/wbi_helpers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from wikibaseintegrator.wbi_backoff import wbi_backoff
1313
from wikibaseintegrator.wbi_config import config
14-
from wikibaseintegrator.wbi_exceptions import MWApiError, SearchError
14+
from wikibaseintegrator.wbi_exceptions import MWApiError, NonExistentEntityError, SearchError
1515

1616
if TYPE_CHECKING:
1717
from wikibaseintegrator.entities.baseentity import BaseEntity
@@ -110,6 +110,13 @@ def mediawiki_api_call(method: str, mediawiki_api_url: str = None, session: Sess
110110
sleep(retry_after)
111111
continue
112112

113+
# non-existent error
114+
if 'code' in json_data['error'] and json_data['error']['code'] == 'no-such-entity':
115+
if 'info' in json_data['error']['code']:
116+
raise NonExistentEntityError(json_data['error']['code']['info'])
117+
else:
118+
raise NonExistentEntityError()
119+
113120
# others case
114121
raise MWApiError(response.json() if response else {})
115122

0 commit comments

Comments
 (0)