Skip to content

feat: enhanced error handing when inspecting processes. Added an intenal lookup table (map) of certain error codes that might show up on win-witr. also changed "Process Ancestry" to "Why It Exists" to match the original witr. etc#52

Merged
supervoidcoder merged 2 commits intomainfrom
better-error-handling
Jan 27, 2026

Conversation

@supervoidcoder
Copy link
Owner

@supervoidcoder supervoidcoder commented Jan 26, 2026

Summary by CodeRabbit

  • New Features

    • Added context-aware error hints that tailor guidance based on your shell/terminal and show parent process info.
    • Added colorized hints for VT-capable terminals with non-colored fallbacks.
    • Updated the ancestry display label to "Why It Exists" for clarity.
  • Reliability

    • Expanded error reporting paths and fallbacks to surface more actionable, contextual troubleshooting hints.

✏️ Tip: You can customize this high-level summary in your review settings.

…nal lookup table (map) of certain error codes that might show up on win-witr. also changed "Process Ancestry" to "Why It Exists" to match the original witr. etc
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 26, 2026

Walkthrough

Adds Windows-specific runtime error hinting and parent-process detection in main.cpp: caches the calling executable, maps common Win32 error codes to human-friendly hints, and surfaces VT-aware colorized or plain hints in OpenProcess/QueryFullProcessImageName error paths (and updates an ancestry label).

Changes

Cohort / File(s) Summary
Error hinting & parent detection
main.cpp
Added thread_local std::string currentParentExe, errorHints map, WideToString declaration, EnsureCurrentParentExe(), and PrintErrorHints(int); integrated hints into OpenProcess/QueryFullProcessImageName error paths; added conio.h; replaced "Process Ancestry" label with "Why It Exists"; ~+172/-9 lines.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Poem

🐰 I peered into processes, traced each shoe,

Left a carrot hint where errors grew,
Colors for terminals, plain words for the rest,
A hop, a nudge—now failures feel less stressed,
Cheers from the burrow; go debug with zest!

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title attempts to describe the main changes but contains typos ('handing' instead of 'handling', 'intenal' instead of 'internal') and uses vague language ('etc'), making it lack polish and clarity. Correct typos ('handing' → 'handling', 'intenal' → 'internal'), remove vague endings like 'etc', and be more concise about the specific improvements (e.g., 'feat: add contextual error hints for process inspection failures on Windows').
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In `@main.cpp`:
- Around line 272-293: The comparisons against currentParentExe are
case-sensitive; normalize currentParentExe to a consistent case (e.g.,
lowercase) before the if/else chain so names like "POWERSHELL.EXE" match; create
a parentLower (or similar) string from currentParentExe using a case-folding
routine (std::transform with ::tolower or an equivalent) and replace uses of
currentParentExe in the subsequent checks (the if/else chain comparing
"powershell.exe", "pwsh.exe", "cmd.exe", "WindowsTerminal.exe", "explorer.exe",
"wsl.exe", "wslhost.exe", "git-bash.exe") with parentLower.
- Around line 503-525: The code logs an error when OpenProcess fails but
continues execution, leading to undefined behavior when
QueryFullProcessImageNameA and CloseHandle are later called with a NULL
hProcess; after handling/logging the failure (the block that sets queryError,
writes to std::cerr and calls PrintErrorHints), return early from the function
to avoid using hProcess, or otherwise skip the subsequent calls that use
hProcess; locate the OpenProcess failure handling around hProcess, pid,
IsVirtualTerminalModeEnabled(), PrintErrorHints(), QueryFullProcessImageNameA(),
and CloseHandle() and insert the early return (or conditional guard) immediately
after the error handling so no further APIs are invoked with a NULL handle.
- Around line 329-341: Remove the dead variable currentParentPid: delete its
declaration and the assignment inside the Process32First/Process32Next loop (the
lines declaring DWORD currentParentPid = 0; and setting currentParentPid =
pe32.th32ParentProcessID;). Keep currentProcessId and existing parentPid logic
intact so behavior is unchanged; no other changes required.
🧹 Nitpick comments (5)
main.cpp (5)

57-57: thread_local is unnecessary for a single-threaded CLI application.

This tool doesn't spawn threads that would each need their own copy of currentParentExe. Using a plain static or just a regular global string would be simpler and avoid TLS overhead.

Suggested fix
-thread_local std::string currentParentExe = ""; // to store the name of our own parent process for error hints
+std::string currentParentExe = ""; // to store the name of our own parent process for error hints

117-121: Consider adding a trailing comma for easier future additions.

The entry for error code 31 lacks a trailing comma. While syntactically valid, adding one makes it easier to add new entries without modifying existing lines.

Suggested fix
 std::unordered_map<int, std::string> errorHints = {
     {5, "Access is denied."},
     {87, "The process or PID doesn't exist."},
-    {31, "This error indicates a driver error, but in win-witr, it often means you are calling a pseudo-process, such as System, Registry, or other processes that only exist in RAM as a kernel process. It is often easy to tell them apart if they lack a .exe extension."}
+    {31, "This error indicates a driver error, but in win-witr, it often means you are calling a pseudo-process, such as System, Registry, or other processes that only exist in RAM as a kernel process. It is often easy to tell them apart if they lack a .exe extension."},

263-269: Minor: double map lookup can be avoided.

Using find() and then operator[] performs two lookups. Use the iterator from find() directly.

Suggested fix
-    if (errorHints.find(errorCode) != errorHints.end()) {
+    auto it = errorHints.find(errorCode);
+    if (it != errorHints.end()) {
         if (IsVirtualTerminalModeEnabled()) {
-            std::cerr << "\033[1;33mHint:\033[0m " << errorHints[errorCode] << std::endl;
+            std::cerr << "\033[1;33mHint:\033[0m " << it->second << std::endl;
         } else {
-            std::cerr << "Hint: " << errorHints[errorCode] << std::endl;
+            std::cerr << "Hint: " << it->second << std::endl;
         }

520-522: Redundant queryError check - it's always true at this point.

queryError is set to true on lines 508/514 before the error is printed, so the check on line 520 will always be true. Either remove the check or restructure the logic if you intend to conditionally call PrintErrorHints.

Simplified version
         }
-        if (queryError) {
         PrintErrorHints(errorCode);
-    }

548-556: Same redundant check pattern as above.

The comment explains the intent well (multiple error paths can occur), but within this specific block, queryError is always true when checked. The variable could be useful if you want to track "any error occurred" across the entire function, but currently each block independently sets and checks it.

@supervoidcoder supervoidcoder merged commit d8e5820 into main Jan 27, 2026
7 checks passed
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