fix(cli): graceful handling for mwclient/requests exceptions#2
Conversation
Catch MwClientError (LoginError, APIError, etc.) and RequestException (ConnectionError, Timeout) from build_site(), method(), maybe_convert_markdown() (site.parse), and iterator iteration. Print a clean message to stderr and exit 1 instead of dumping a raw traceback. - Add _handle_runtime_error() helper; optionally import requests.exceptions - Wrap maybe_convert_markdown in same try/except as method() so parse() failures are handled - Tests: APIError, ConnectionError, and iterator mid-stream raise Made-with: Cursor
Greptile SummaryThis PR adds graceful CLI error handling for runtime failures from
Confidence Score: 5/5The changes are narrowly scoped to CLI runtime error handling and accompanying tests. The implementation covers the intended mwclient and requests failure paths without introducing additional concerns in the reviewed diff.
What T-Rex did
Reviews (2): Last reviewed commit: "fix(cli): graceful handling for mwclient..." | Re-trigger Greptile |
| def _handle_runtime_error(e: Exception) -> int: | ||
| """Print a graceful CLI message for mwclient/requests errors; return exit code 1.""" | ||
| if isinstance(e, mwclient_errors.MwClientError): | ||
| print(str(e), file=sys.stderr) | ||
| return 1 | ||
| if requests_exceptions is not None and isinstance( | ||
| e, requests_exceptions.RequestException | ||
| ): | ||
| print(str(e), file=sys.stderr) | ||
| return 1 | ||
| print(f"Error: {e}", file=sys.stderr) | ||
| return 1 |
There was a problem hiding this comment.
Broad
except Exception silently swallows programming errors
All three new try/except blocks catch bare Exception, so any unexpected bug inside build_target, method(), maybe_convert_markdown, or the iterator loop (e.g. an AttributeError, TypeError, or NameError in the CLI code itself) will be caught by the third branch in _handle_runtime_error and printed as "Error: <msg>" with no traceback. This makes accidental regressions hard to distinguish from expected network/API errors. Consider re-raising exceptions that are neither MwClientError nor RequestException (or add a --debug flag to preserve tracebacks for unknown exception types).
Prompt To Fix With AI
This is a comment left during a code review.
Path: mwclient_cli/cli.py
Line: 24-35
Comment:
**Broad `except Exception` silently swallows programming errors**
All three new try/except blocks catch bare `Exception`, so any unexpected bug inside `build_target`, `method()`, `maybe_convert_markdown`, or the iterator loop (e.g. an `AttributeError`, `TypeError`, or `NameError` in the CLI code itself) will be caught by the third branch in `_handle_runtime_error` and printed as `"Error: <msg>"` with no traceback. This makes accidental regressions hard to distinguish from expected network/API errors. Consider re-raising exceptions that are neither `MwClientError` nor `RequestException` (or add a `--debug` flag to preserve tracebacks for unknown exception types).
How can I resolve this? If you propose a fix, please make it concise.
Catch MwClientError (LoginError, APIError, etc.) and RequestException (ConnectionError, Timeout) from build_site(), method(), maybe_convert_markdown() (site.parse), and iterator iteration. Print a clean message to stderr and exit 1 instead of dumping a raw traceback.
Made with Cursor