Raise edit API errors with response details
This commit is contained in:
parent
9486d9cb8a
commit
bd0c1a26c2
2 changed files with 58 additions and 5 deletions
|
|
@ -1,7 +1,6 @@
|
|||
"""Interface with the mediawiki API."""
|
||||
|
||||
import typing
|
||||
from pprint import pprint
|
||||
from typing import Any, cast
|
||||
|
||||
import requests
|
||||
|
|
@ -12,6 +11,17 @@ from . import mediawiki_oauth
|
|||
class APIError(Exception):
|
||||
"""Unexpected response from the MediaWiki API."""
|
||||
|
||||
|
||||
def _format_api_error(data: dict[str, Any]) -> str:
|
||||
"""Format a MediaWiki API error response."""
|
||||
error = data.get("error")
|
||||
if isinstance(error, dict):
|
||||
message = error.get("info") or error.get("code")
|
||||
if message:
|
||||
return str(message)
|
||||
return f"Unexpected MediaWiki API response: {data!r}"
|
||||
|
||||
|
||||
wiki_hostname = "en.wikipedia.org"
|
||||
wiki_api_php = f"https://{wiki_hostname}/w/api.php"
|
||||
user_agent = "add-links/0.1"
|
||||
|
|
@ -103,8 +113,5 @@ def edit_page(
|
|||
}
|
||||
ret = call(params, timeout=30)
|
||||
if "edit" not in ret:
|
||||
print("params")
|
||||
pprint(params)
|
||||
print()
|
||||
pprint(ret)
|
||||
raise APIError(_format_api_error(ret))
|
||||
return typing.cast(str, ret["edit"])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue