fix(utils): decode CommandError stdout safely so __str__ never raises#1316
Conversation
`CommandError.__str__` decoded captured stdout as strict UTF-8, so any non-UTF-8 bytes from a subprocess (legacy locales, binary diff fragments, truncated multi-byte sequences) would turn `str(error)` into a `UnicodeDecodeError`. Every error-formatting site is affected — the top-level CLI handler in `cli.py:104`, log messages, etc. — converting the failure into an unhandled traceback instead of a clean message. Switch to `decode(errors="replace")`. Invalid bytes become the U+FFFD replacement character; all other formatting is preserved. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Change-Id: I3e3fd853218a6c4294b17e9529794320288124e5
|
This pull request is part of a Mergify stack:
|
Merge ProtectionsYour pull request matches the following merge protections and will not be merged until they are valid. 🟢 🤖 Continuous IntegrationWonderful, this rule succeeded.
🟢 👀 Review RequirementsWonderful, this rule succeeded.
🟢 Enforce conventional commitWonderful, this rule succeeded.Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
🟢 🔎 ReviewsWonderful, this rule succeeded.
🟢 📕 PR descriptionWonderful, this rule succeeded.
|
There was a problem hiding this comment.
Pull request overview
This PR hardens error formatting for subprocess failures by ensuring CommandError.__str__() cannot raise UnicodeDecodeError when captured stdout contains non‑UTF‑8 bytes, preventing secondary tracebacks in CLI/log error paths.
Changes:
- Decode
CommandError.stdoutusingerrors="replace"to guarantee safe stringification. - Add a regression test covering non‑UTF‑8 stdout handling in
CommandError.__str__().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
mergify_cli/utils.py |
Makes CommandError.__str__() robust to invalid UTF‑8 bytes by decoding with replacement. |
mergify_cli/tests/test_utils.py |
Adds a regression test ensuring str(CommandError) doesn’t raise on non‑UTF‑8 stdout. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Merge Queue Status
This pull request spent 7 minutes 6 seconds in the queue, including 6 minutes 45 seconds running CI. Required conditions to merge
|
…#1315) The exception handler around `fetch_old_pr_heads` previously did a silent `pass`, leaving every revision-history entry stamped "unknown" with no clue why. When a real failure happens (fork remote without `refs/pull/N/head`, network error, missing permissions, …) the user has no signal that anything went wrong — they only see the symptom in the PR comment days later. Log the underlying error in orange so the cause is visible at push time. The exception text is run through `rich.markup.escape` so any `[`/`]` in the git error message is rendered literally rather than parsed as Rich markup. Behaviour stays non-fatal: classification still falls back to "unknown" and the push proceeds. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Depends-On: #1316
CommandError.__str__decoded captured stdout as strict UTF-8, so anynon-UTF-8 bytes from a subprocess (legacy locales, binary diff fragments,
truncated multi-byte sequences) would turn
str(error)into aUnicodeDecodeError. Every error-formatting site is affected — thetop-level CLI handler in
cli.py:104, log messages, etc. — convertingthe failure into an unhandled traceback instead of a clean message.
Switch to
decode(errors="replace"). Invalid bytes become the U+FFFDreplacement character; all other formatting is preserved.
Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com