Skip to content

Commit

Permalink
Add NonExistentEntityError exception (#331)
Browse files Browse the repository at this point in the history
* Add NonExistentEntityError exception

Co-Authored-By: Dennis Priskorn <dennis@priskorn.se>

* Add missing Exception

Co-authored-by: Dennis Priskorn <dennis@priskorn.se>
  • Loading branch information
LeMyst and dpriskorn authored May 5, 2022
1 parent b3c16ca commit e236674
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions test/test_entity_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from wikibaseintegrator import WikibaseIntegrator
from wikibaseintegrator.datatypes import BaseDataType, Item
from wikibaseintegrator.wbi_config import config as wbi_config
from wikibaseintegrator.wbi_exceptions import NonExistentEntityError

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

Expand Down Expand Up @@ -33,6 +34,10 @@ def test_get(self):
with self.assertRaises(ValueError):
wbi.item.get(-1)

# Test with negative id
with self.assertRaises(NonExistentEntityError):
wbi.item.get("Q99999999999999")

def test_get_json(self):
assert wbi.item.get('Q582').get_json()['labels']['fr']['value'] == 'Villeurbanne'

Expand Down
12 changes: 9 additions & 3 deletions wikibaseintegrator/entities/baseentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from wikibaseintegrator.datatypes import BaseDataType
from wikibaseintegrator.models.claims import Claim, Claims
from wikibaseintegrator.wbi_enums import ActionIfExists
from wikibaseintegrator.wbi_exceptions import MWApiError, NonUniqueLabelDescriptionPairError
from wikibaseintegrator.wbi_exceptions import MWApiError, NonExistentEntityError, NonUniqueLabelDescriptionPairError
from wikibaseintegrator.wbi_helpers import delete_page, mediawiki_api_call_helper
from wikibaseintegrator.wbi_login import _Login

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

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

if 'title' in json_data: # TODO: 1.35 compatibility
self.title = str(json_data['title'])
Expand Down
4 changes: 4 additions & 0 deletions wikibaseintegrator/wbi_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ class CorePropIntegrityException(Exception):

class MergeError(Exception):
pass


class NonExistentEntityError(Exception):
pass
9 changes: 8 additions & 1 deletion wikibaseintegrator/wbi_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from wikibaseintegrator.wbi_backoff import wbi_backoff
from wikibaseintegrator.wbi_config import config
from wikibaseintegrator.wbi_exceptions import MWApiError, SearchError
from wikibaseintegrator.wbi_exceptions import MWApiError, NonExistentEntityError, SearchError

if TYPE_CHECKING:
from wikibaseintegrator.entities.baseentity import BaseEntity
Expand Down Expand Up @@ -110,6 +110,13 @@ def mediawiki_api_call(method: str, mediawiki_api_url: str = None, session: Sess
sleep(retry_after)
continue

# non-existent error
if 'code' in json_data['error'] and json_data['error']['code'] == 'no-such-entity':
if 'info' in json_data['error']['code']:
raise NonExistentEntityError(json_data['error']['code']['info'])
else:
raise NonExistentEntityError()

# others case
raise MWApiError(response.json() if response else {})

Expand Down

0 comments on commit e236674

Please sign in to comment.