Show full Wikipedia 429 error message

This commit is contained in:
Edward Betts 2026-05-14 10:15:19 +01:00
parent fd59e2a9a4
commit 3ba7eaefd0
3 changed files with 45 additions and 4 deletions

View file

@ -37,6 +37,17 @@ class Hit(typing.TypedDict):
timestamp: str
def render_error(message: str) -> str:
"""Render shared error page."""
return flask.render_template("error.html", message=message)
def render_mediawiki_error(error: Exception, *, prefix: str | None = None) -> str:
"""Render MediaWiki errors."""
message = f"{prefix}: {error}" if prefix else str(error)
return render_error(message)
def load_examples() -> list[dict[str, str | int]]:
"""Load examples."""
return [json.loads(line) for line in open("examples")]
@ -203,7 +214,7 @@ def start_oauth() -> Response:
fetch_response = oauth.fetch_request_token(request_token_url)
except TokenRequestDenied as e:
return flask.make_response(
flask.render_template("error.html", message=str(e)), 502
render_error(str(e)), 502
)
flask.session["owner_key"] = fetch_response.get("oauth_token")
@ -325,7 +336,7 @@ def handle_post(url_title: str) -> Response:
return flask.redirect(flask.url_for("start_oauth", next=next_url))
except (mediawiki_api.APIError, api.MediawikiError) as e:
return flask.make_response(
flask.render_template("error.html", message=f"Save failed: {e}"), 502
render_mediawiki_error(e, prefix="Save failed"), 502
)
flask.session["saves"] = flask.session.get("saves", 0) + 1
saves_by_title: dict[str, int] = flask.session.get("saves_by_title", {})
@ -357,7 +368,7 @@ def article_page(url_title: str) -> str | Response:
_no_link_count, hits = search_no_link(from_title, redirect_to)
except api.MediawikiError as e:
return flask.make_response(
flask.render_template("error.html", message=str(e)), 502
render_mediawiki_error(e), 502
)
# Filter out candidates already processed this session