Repository navigation
fix: check API error response and set Redis cache after successful save - #86
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
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.
| result = response.json() | ||
| if "error" in result: | ||
| logger.critical( | ||
| f"API error saving {self.wiki_properties.mid}: {result['error']}" | ||
| ) | ||
| return |
There was a problem hiding this comment.
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
- 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.
| logger.info( | ||
| f"Updating {self.wiki_properties.mid} took {(perf_counter() - start):.1f} s" | ||
| ) | ||
| self.redis.set(self.wiki_properties.redis_key, 1) |
There was a problem hiding this comment.
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
- 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.
Two bugs in
bot.py'ssave()method: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.Redis key not set after successful save:
self.redis.set()was only called when there were no new claims. After a successfulwbeditentitycall, the Redis key was never set, causing the bot to re-process the same page on subsequent runs.