Skip to content

Commit

Permalink
Improve/Fix Exceptions (#450)
Browse files Browse the repository at this point in the history
* Exception: Add an attribute with the error dictionnary

* Fix wrong variable for MWApiError

* Fix mypy error

* Remove default value
  • Loading branch information
LeMyst authored Nov 21, 2022
1 parent b4857be commit 519deef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions wikibaseintegrator/wbi_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class MWApiError(Exception):
"""
Base class for MediaWiki API error handling
"""
error_dict: Dict[str, Any]
code: str
info: str
messages: List[Dict[str, Any]]
Expand Down Expand Up @@ -41,15 +42,17 @@ def get_languages(self) -> List[str]:
)

def __init__(self, error_dict: Dict[str, Any]):
if 'info' in error_dict:
self.info = error_dict['info']
self.error_dict = error_dict

if 'info' in self.error_dict:
self.info = self.error_dict['info']
else:
self.info = 'MWApiError'
super().__init__(self.info)
self.code = error_dict['code'] if 'code' in error_dict else 'wikibaseintegrator-missing-error-code'
if 'messages' in error_dict:
self.messages = error_dict['messages']
self.messages_names = [message['name'] for message in error_dict['messages']]
self.code = self.error_dict['code'] if 'code' in error_dict else 'wikibaseintegrator-missing-error-code'
if 'messages' in self.error_dict:
self.messages = self.error_dict['messages']
self.messages_names = [message['name'] for message in self.error_dict['messages']]
else:
self.messages = [{'html': {'*': 'WikibaseIntegrator: missing message from HTML return.'}, 'name': 'wikibaseintegrator-missing-messages'}]
self.messages_names = ['wikibaseintegrator-missing-messages']
Expand Down
2 changes: 1 addition & 1 deletion wikibaseintegrator/wbi_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def mediawiki_api_call(method: str, mediawiki_api_url: Optional[str] = None, ses
raise ModificationFailed(json_data['error'])

# others case
raise MWApiError(json_data)
raise MWApiError(json_data['error'])

# there is no error or waiting. break out of this loop and parse response
break
Expand Down

0 comments on commit 519deef

Please sign in to comment.