Skip to content

fix: check API error response and set Redis cache after successful save - #86

Merged
DaxServer merged 1 commit into
mainfrom
fix/save-bug
Apr 11, 2026
Merged

DaxServer merged 1 commit into
mainfrom
fix/save-bug

Conversation

@DaxServer

Copy link
Copy Markdown
Owner

Two bugs in bot.py's save() method:

  1. API errors silently ignored: response.raise_for_status() only catches HTTP-level errors (4xx/5xx). The MediaWiki API returns application errors as HTTP 200 with {"error": {...}} in the body. A failed save was indistinguishable from success.

  2. Redis key not set after successful save: self.redis.set() was only called when there were no new claims. After a successful wbeditentity call, the Redis key was never set, causing the bot to re-process the same page on subsequent runs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@DaxServer
DaxServer merged commit 391a520 into main Apr 11, 2026
5 checks passed
@DaxServer
DaxServer deleted the fix/save-bug branch April 11, 2026 12:28

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the save method in bot.py by adding explicit checks for MediaWiki API errors in JSON responses and updating the Redis state to track processed pages. Feedback suggests centralizing the API error handling logic and creating a helper method for Redis state management to reduce code duplication and improve maintainability across the codebase.

Comment thread src/wikibots/lib/bot.py
Comment on lines +309 to +314
result = response.json()
if "error" in result:
logger.critical(
f"API error saving {self.wiki_properties.mid}: {result['error']}"
)
return

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While this check correctly identifies MediaWiki API errors for the wbeditentity call, the same issue (ignoring application-level errors on HTTP 200) exists in the _commons_api helper method and other manual API calls (like in null_edit). To ensure consistent error handling across the entire bot and adhere to the general rule of using helper methods for common tasks, this error-checking logic should be centralized within _commons_api. This would protect all API interactions, not just the save operation.

References
  1. Always use helper methods for common tasks like wikicode parsing, template filtering, parameter extraction, logging, and Redis state management to improve maintainability and reduce code duplication.

Comment thread src/wikibots/lib/bot.py
logger.info(
f"Updating {self.wiki_properties.mid} took {(perf_counter() - start):.1f} s"
)
self.redis.set(self.wiki_properties.redis_key, 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This line introduces further duplication of the Redis state management logic, which is already repeated in several places (lines 196, 207, and 282). Per the general rules, you should implement a helper method (e.g., _mark_as_processed) to handle marking a page as completed in Redis. This improves maintainability and ensures consistency in how the bot tracks its progress.

References
  1. Always use helper methods for common tasks like wikicode parsing, template filtering, parameter extraction, logging, and Redis state management to improve maintainability and reduce code duplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant