LeMyst / WikibaseIntegrator

A Python module to manipulate data on a Wikibase instance (like Wikidata) through the MediaWiki Wikibase API and the Wikibase SPARQL endpoint.
MIT License
67 stars 13 forks source link

Improve exception with multiples messages #459

Open LeMyst opened 1 year ago

LeMyst commented 1 year ago

It seems the exception don't give enough informations in some cases.

For my example, I tried to create a new entity (a property) with already used Labels on the same wikibase instance. Wikibase don't allow this and simply return a "The save have failed" with the default MWApiError:

ERROR:wikibaseintegrator.entities.baseentity:Error while writing to the Wikibase instance
wikibaseintegrator.wbi_exceptions.MWApiError: 'The save has failed.'

but the error returned by WB is really explicit:

{'error': {'*': 'See https://test.wikidata.org/w/api.php for API usage. '
                'Subscribe to the mediawiki-api-announce mailing list at '
                '<https://lists.wikimedia.org/postorius/lists/mediawiki-api-announce.lists.wikimedia.org/> '
                'for notice of API deprecations and breaking changes.',
           'code': 'failed-save',
           'info': 'The save has failed.',
           'messages': [{'html': {'*': 'The save has failed.'},
                         'name': 'wikibase-api-failed-save',
                         'parameters': []},
                        {'html': {'*': 'Property <a '
                                       'href="/wiki/Property:P97083" '
                                       'title="Property:P97083">P97083</a> '
                                       'already has label "Leksem-eksempel for '
                                       'Wikidata-egenskap" associated with '
                                       'language code nb.'},
                         'name': 'wikibase-validator-label-conflict',
                         'parameters': ['Leksem-eksempel for Wikidata-egenskap',
                                        'nb',
                                        '[[Property:P97083|P97083]]']},
                        {'html': {'*': 'Property <a '
                                       'href="/wiki/Property:P97083" '
                                       'title="Property:P97083">P97083</a> '
                                       'already has label "Wikidata property '
                                       'example for lexemes" associated with '
                                       'language code en.'},
                         'name': 'wikibase-validator-label-conflict',
                         'parameters': ['Wikidata property example for lexemes',
                                        'en',
                                        '[[Property:P97083|P97083]]']},
                        {'html': {'*': 'Property <a '
                                       'href="/wiki/Property:P97083" '
                                       'title="Property:P97083">P97083</a> '
                                       'already has label '
                                       '"Wikidata-Eigenschaftsbeispiel für '
                                       'Lexeme" associated with language code '
                                       'de.'},
                         'name': 'wikibase-validator-label-conflict',
                         'parameters': ['Wikidata-Eigenschaftsbeispiel für '
                                        'Lexeme',
                                        'de',
                                        '[[Property:P97083|P97083]]']},
[...]
                        {'html': {'*': 'Property <a '
                                       'href="/wiki/Property:P97083" '
                                       'title="Property:P97083">P97083</a> '
                                       'already has label "詞位類維基數據屬性範例" '
                                       'associated with language code zh-hk.'},
                         'name': 'wikibase-validator-label-conflict',
                         'parameters': ['詞位類維基數據屬性範例',
                                        'zh-hk',
                                        '[[Property:P97083|P97083]]']},
                        {'html': {'*': 'Property <a '
                                       'href="/wiki/Property:P97083" '
                                       'title="Property:P97083">P97083</a> '
                                       'already has label "詞位類維基數據屬性範例" '
                                       'associated with language code zh-tw.'},
                         'name': 'wikibase-validator-label-conflict',
                         'parameters': ['詞位類維基數據屬性範例',
                                        'zh-tw',
                                        '[[Property:P97083|P97083]]']},
                        {'html': {'*': 'Property <a '
                                       'href="/wiki/Property:P97083" '
                                       'title="Property:P97083">P97083</a> '
                                       'already has label "語彙素内での使用例" '
                                       'associated with language code ja.'},
                         'name': 'wikibase-validator-label-conflict',
                         'parameters': ['語彙素内での使用例',
                                        'ja',
                                        '[[Property:P97083|P97083]]']},
                        {'html': {'*': 'Property <a '
                                       'href="/wiki/Property:P97083" '
                                       'title="Property:P97083">P97083</a> '
                                       'already has label "词位类维基数据属性示例" '
                                       'associated with language code '
                                       'zh-hans.'},
                         'name': 'wikibase-validator-label-conflict',
                         'parameters': ['词位类维基数据属性示例',
                                        'zh-hans',
                                        '[[Property:P97083|P97083]]']},
                        {'html': {'*': 'Property <a '
                                       'href="/wiki/Property:P97083" '
                                       'title="Property:P97083">P97083</a> '
                                       'already has label "어휘소를 위한 위키데이터 속성 '
                                       '예시" associated with language code ko.'},
                         'name': 'wikibase-validator-label-conflict',
                         'parameters': ['어휘소를 위한 위키데이터 속성 예시',
                                        'ko',
                                        '[[Property:P97083|P97083]]']}]},
 'servedby': 'mw1377'}

WBI exceptions must be improved to give more informations.

WolfgangFahl commented 1 year ago

Another example:

   raise ModificationFailed(json_data['error'])
wikibaseintegrator.wbi_exceptions.ModificationFailed: 'Item [[Q116472444|Q116472444]] already has label "Xiao Liu" associated with language code en, using the same description text.'

see