Skip to content

Latest commit

 

History

History

Strict-mode-ErrorRecord-formatting

ErrorRecord formatting may fail in the strict mode in the default host

  • Fixed in v6-RC

Some internal code of ErrorRecord formatting is not well designed for the strict mode with some hosts. The code uses $host.UI.RawUI.BufferSize.Width but this property may not exist, for example in the default host.

The scripts below create an instance of [PowerShell] and invoke some code in its runspace. The code throws an error and in the catch block tries to output formatted information about the error. The formatting fails.

These scripts use Out-String and Format-List. Some other commands that trigger formatting are Out-File, Format-Custom, Format-Table, Format-Wide.

The scripts work fine if this line is removed

Set-StrictMode -Version 2

If the strict mode is required and a script is designed to work in any host then standard formatting of errors should be avoided.

The script Test-3.manual.formatting.ps1 shows how to manually format an error in a way similar to Out-String.