2

I've read about differences of echo (which is a Write-Output) and Write-Error but I have still issues with exceptions and stack traces.

The following sample demonstrates my issue:

$ErrorActionPreference = "stop"
trap  {
    echo "Trap encountered. Exiting with 1. See errors below."
    echo $_ # This is a test
    Write-Error -ErrorRecord $_

    exit 1
}

BROKEN_COMMAND

I won't post the output as it is localized but the gist is:

  • echo outputs "BROKEN_COMMAND" was not found to be a CmdLet etc and outputs the callstack of the exact location where the issue was found, above would be line 10 or so.
  • Write-Error outputs the line "BROKEN_COMMAND" was not found to be a CmdLet too but then the callstack shows actually the line where the Write-Error statement is and not the proper callstack (line 5)

How can I properly write to the error channel? I've tried omitting -ErrorRecord but no effect.

I need the error channel for errors so dumping everything in echo and then writing "there has been an error" in the error channel is not a good option.


Update: I tested @MathiasR.Jessen hint with throw (actually throw $_). This results however in an immediate exit of the program without a useful exit code in $LASTEXITCODE, which I require to properly signal the end. My exit 1 is not executed and the throw terminates without proper exit code.

Samuel
  • 6,126
  • 35
  • 70
  • interesting reading: http://www.powershellmagazine.com/2011/09/14/custom-errors/ – CB. Aug 31 '15 at 12:27
  • Re-throw the error using `throw` (with no arguments) instead of `Write-Error $_` if you want to preserve the original callstack – Mathias R. Jessen Aug 31 '15 at 15:23
  • @MathiasR.Jessen throw did not work. Writing `throw $_` however fixed it :) I did not think about throw as I was in the trap and assumed recursion here. – Samuel Sep 01 '15 at 06:37
  • @MathiasR.Jessen this is actually still useless with `throw $_` since it exits my program there is no proper exit code specified, and I need exit codes when exceptions exit, this is quirky powershell behaviour. – Samuel Sep 01 '15 at 06:45

1 Answers1

1

see Writing to error stream in Powershell using Write-Error

the write-error cmdlet will display your message followed by the line number of the write-error cmdlet itself.

in the linked question it was said that this behaviour chnaged at some point.

in either case.. try this instead hidin in Get-Help About_Preference_Variables is mention of the $ErrorView variable

NormalView (default)

Trap encountered. Exiting with 1. See errors below.
BROKEN_COMMAND : The term 'BROKEN_COMMAND' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.
At C:\errors.ps1:11 char:1
+ BROKEN_COMMAND
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (BROKEN_COMMAND:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

C:\errors.ps1 : The term 'BROKEN_COMMAND' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again.
At C:\errors.ps1:6 char:6
+      Write-Error -ErrorRecord $_
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (BROKEN_COMMAND:String) [Write-Error], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,errors.ps1

but if you set $ErrorView = "CategoryView" you get

Trap encountered. Exiting with 1. See errors below.
ObjectNotFound: (BROKEN_COMMAND:String) [], CommandNotFoundException
ObjectNotFound: (BROKEN_COMMAND:String) [Write-Error], CommandNotFoundException

perhaps that is more useful

you may want to change your echo $_ to echo $_.tostring()

also see https://blogs.msdn.microsoft.com/powershell/2006/06/21/errorviewcategoryview/

its a shame you cant write your own error form like log4

Community
  • 1
  • 1
ShoeLace
  • 3,476
  • 2
  • 30
  • 44