question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Write-Error should use positional binding to -ErrorRecord and -Exception

See original GitHub issue

When using the parameter sets selected by -ErrorRecord / -Exception of Write-Error, it makes sense to bind (the first) positional argument of types (derived from) System.Management.Automation.ErrorRecord / System.Exception to those parameters.

Currently, that doesn’t happen, because these parameters lack the Position=0 attribute field in their `Parameter attributes.

The result is that using something like Write-Error $_ in an apparent effort to pass the System.Management.Automation.ErrorRecord instance in $_ through, you end up with the equivalent of:

Write-Error -Message $_

rather than the more sensible - and probably expected:

Write-Error -ErrorRecord $_

While the two resulting error records ultimately contain the same message (description), the specifics of the input error record are lost.

The same applies analogously to -Exception.

The fix is trivial: Add Position = 0 to the following two locations:

https://github.com/PowerShell/PowerShell/blob/d2c04f3eef071b244838ba2dfa001dd4d5731843/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Write.cs#L235-L236

https://github.com/PowerShell/PowerShell/blob/d2c04f3eef071b244838ba2dfa001dd4d5731843/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Write.cs#L217-L218

Steps to reproduce

Run the following tests:

# ErrorRecord
try { [int]::parse('foo') } catch {}; (Write-Error $Error[0] 2>&1).Exception.GetType().FullName | Should -Be System.Management.Automation.MethodInvocationException

# Exception
try { [int]::parse('foo') } catch {}; (Write-Error $Error[0].Exception.InnerException 2>&1).Exception.GetType().FullName | Should -Be System.FormatException

# Make sure that a string still binds to -Message.
try { [int]::parse('foo') } catch {}; (Write-Error "$($Error[0])" 2>&1).Exception.GetType().FullName | Should -Be Microsoft.PowerShell.Commands.WriteErrorException

Expected behavior

All tests should pass.

Actual behavior

The first two tests fail, because the positional arguments bind to -Message, resulting in a generic Microsoft.PowerShell.Commands.WriteErrorException exception wrapper.

Environment data

PowerShell Core 7.0.0-preview.4

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ThomasNietocommented, Oct 9, 2019

I’ll work on this one.

1reaction
vexx32commented, Oct 8, 2019

Excellent! This seems like a minor enhancement-type change. Marking it for Hacktoberfest. 😁

Read more comments on GitHub >

github_iconTop Results From Across the Web

Write-Error Position 0 for parameters · Issue #6771
... in the PowerShell/PowerShell repo: Write-Error should use positional binding to -ErrorRecord and -Exception PowerShell/PowerShell#10739 ...
Read more >
What's the right way to emit errors in powershell module ...
The only workaround at present is to make sure that your function/script is an advanced one and to use $PSCmdlet.WriteError() ; from a...
Read more >
Write-Error (Microsoft.PowerShell.Utility)
To create an exception object, use a hash table or use the New-Object cmdlet. Type: Exception. Position: 0. Default value: None. Accept pipeline...
Read more >
PowerShell Write-Error Without Writing Stack Trace
Solution. To resolve the problem I wrote a Write-ErrorMessage function. The essence of the function was to write out the error message using...
Read more >
Custom errors
Write-Error writes non-terminating errors while the throw statement writes ... The Exception that will be associated with the ErrorRecord.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found