Skip to content

Fix inconsistent exception message in -replace operator - #12388

Merged
Aditya Patwardhan (adityapatwardhan) merged 2 commits into
PowerShell:masterfrom
jackdcasey:ReplaceOperator_VerboseExceptionMessage
Apr 28, 2020
Merged

Aditya Patwardhan (adityapatwardhan) merged 2 commits into
PowerShell:masterfrom
jackdcasey:ReplaceOperator_VerboseExceptionMessage

Conversation

@jackdcasey

@jackdcasey Jack Casey (jackdcasey) commented Apr 20, 2020 •

Copy link
Copy Markdown
Contributor

PR Summary

Fix inconsistent exception message in -replace operator

PR Context

The exception message returned when passing three or more values to -replace, -ireplace or -creplace is not consistent with the actual operator used:

PS> "foo" -replace "f", "j", "h"
InvalidOperation: The -ireplace operator allows only two elements to follow it, not 3.
PS> "foo" -ireplace "f", "j", "h"
InvalidOperation: The -ireplace operator allows only two elements to follow it, not 3.
PS> "foo" -creplace "f", "j", "h"
InvalidOperation: The -replace operator allows only two elements to follow it, not 3.

My proposed changes would change the exception messages to:

PS> "foo" -replace "f", "j", "h"
InvalidOperation: The -ireplace/-replace operator allows only two elements to follow it, not 3.
PS> "foo" -ireplace "f", "j", "h"
InvalidOperation: The -ireplace/-replace operator allows only two elements to follow it, not 3.
PS> "foo" -creplace "f", "j", "h"
InvalidOperation: The -creplace operator allows only two elements to follow it, not 3.

Due to the way these parameters are passed as a boolean, we don't have visibility into exactly what parameter was used. For this reason -ireplace and -replace "share" the same error message.

Closes #12333

PR Checklist

@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Apr 21, 2020
@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Apr 26, 2020
@adityapatwardhan
Aditya Patwardhan (adityapatwardhan) merged commit 767899d into PowerShell:master Apr 28, 2020
@iSazonov

Copy link
Copy Markdown
Collaborator

Jack Casey (@jackdcasey) Thanks for your contribution!

@jackdcasey
Jack Casey (jackdcasey) deleted the ReplaceOperator_VerboseExceptionMessage branch April 28, 2020 17:58
@ghost

Copy link
Copy Markdown

🎉v7.1.0-preview.3 has been released which incorporates this pull request.:tada:

Handy links:

@TravisEz13

Copy link
Copy Markdown
Member

I don't think this meets the bar to backport

@TravisEz13

Copy link
Copy Markdown
Member

Triage determined this does not meet the bar for backport

@iSazonov

Copy link
Copy Markdown
Collaborator

Should we set 7.1.0-preview.3 for history?

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
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.

Replace Operator InvalidOperation Exception Message

5 participants