X Tutup
The Wayback Machine - https://web.archive.org/web/20250624132115/https://github.com/PowerShell/PowerShell/pull/24949
Skip to content

Stringify ErrorRecord with empty exception message to empty string #24949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2025

Conversation

MatejKafka
Copy link
Contributor

PR Summary

Fixes #23950.

PR Context

When a native process writes a line to stderr, PowerShell wraps it in an ErrorRecord. Users often want to merge the stdout and stderr streams, at which point they need to convert the ErrorRecord from stderr back to a string. However, before this PR, an ErrorRecord with an empty exception message string stringifies to the name of the exception (System.Management.Automation.RemoteException for this specific scenario), which requires the user to use various workarounds (e.g. grabbing .Exception.Message manually) to get the original string.

Reproduction:

$(try {throw ""} catch {$_}) | % {"$_"} # System.Management.Automation.RuntimeException

This PR makes .ToString() behave consistently even for empty exception messages, so just running .ToString() on items from stderr should be enough to get back the original strings. While this is technically a breaking change, I'm pretty sure it falls into bucket 3 (unlikely gray area).

PR Checklist

@iSazonov
Copy link
Collaborator

iSazonov commented Feb 5, 2025

@mklement0 Does the PR fix your issue?

@mklement0
Copy link
Contributor

LGTM. Thanks for tackling this, @MatejKafka.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Feb 5, 2025
@MatejKafka
Copy link
Contributor Author

MatejKafka commented Feb 6, 2025

Updated a Pester test that was using -IsNullOrEmpty on an ErrorRecord (which implicitly converts to string before doing the comparison).

@MatejKafka
Copy link
Contributor Author

@iSazonov Would you please re-approve the CI?

@iSazonov

This comment was marked as outdated.

This comment was marked as outdated.

@MatejKafka
Copy link
Contributor Author

MatejKafka commented Feb 7, 2025

@iSazonov I don't think the failed tests are related to this PR – they're in PSResourceGet, and the code where the error seems to come from does not work with other ErrorRecords at all. I'd suspect that something is broken with the test repository setup.

@iSazonov
Copy link
Collaborator

iSazonov commented Feb 8, 2025

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@iSazonov iSazonov closed this Feb 8, 2025
@iSazonov iSazonov reopened this Feb 8, 2025
@iSazonov
Copy link
Collaborator

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@iSazonov iSazonov changed the title Stringify ErrorRecord with empty exception message to empty string (#23950) Stringify ErrorRecord with empty exception message to empty string Feb 10, 2025
@iSazonov iSazonov merged commit 9071eac into PowerShell:master Feb 10, 2025
38 of 41 checks passed
@iSazonov iSazonov self-assigned this Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Empty ErrorRecord-wrapped stderr lines stringify as the exception type name rather than as the empty string
3 participants
X Tutup