$ERRORVIEW="CATEGORYVIEW"

<Edited 7/2/2006 with tags and categories>
PSMDTAG:FAQ: What is $ErrorView?
PSMDTAG:FAQ: How do I get error details?


Have you tried out $ErrorView=”CategoryView” yet? 


Remember –  errors are just objects.  Given that they are objects, we render a subset of their properties to the console. 


We provide 2 views: Normal and “CategoryView”.  The normal view provides semi-verbose details of what went wrong where.  This is great for beginners and casual users. 


That said, if you are an production operator and are doing the same tasks over and over day in and day out, you are going to have a well known set of errors that you encounter and this verbosity can be a problem.  That is why we have categoryView. (This is another one of those features that fall into the bucket of “We want to be as Production-Oriented as the AS400/CL and VMS DCL”.)


PSMDTAG:PHILOSOPHY:  Provide users the option of being pithy or verbose.
PSMDTAG:PHILOSOPHY:  Remember that some your users will be working on machines doing business worth millions or billions – code accordingly.


When you have CategoryView, we map all errors to a fixed set of pithy, structured, parameterized errors that are easy to parse visually and understand what is going on.  It is easier to see then to explain.  Here are 3 errors shown using the normal view:



PS> asdf
‘asdf’ is not recognized as a cmdlet, function, operable program, or script file.
At line:1 char:4
+ asdf <<<<

PS> stop-process -test
Stop-Process : A parameter cannot be found that matches parameter name ‘test’.
At line:1 char:18
+ stop-process -test <<<<

PS> stop-process 13
Stop-Process : No process with process ID 13 was found.
At line:1 char:13
+ stop-process  <<<< 13


Now we’ll switch views and show the same errors.  Notice that you get much less info but that the info you get is very regular.  The first field is one of about 2 dozen error categories.  Next (in ()s) is the arguement identifer and the type of that arguement.  Next (in []s)  is the name of the cmdlet that was running when the error occurred.  Last is the .NET Exception name that generated the error.


PS> $errorview=”CategoryView
PS> asdf
ObjectNotFound: (asdf:String) [], CommandNotFoundException
PS> stop-process -test
InvalidArgument: (:) [Stop-Process], ParameterBindingException
PS> $errorview=”CategoryView”
PS> stop-process 13
ObjectNotFound: (13:Int32) [Stop-Process], ProcessCommandException


Remember that this just affects the VIEW of the error. You still have the error object in $error with all the details so if you are in categoryview and need more info you just do the following (btw: notice that you have to use -FORCE in the formatting command [the reason why would take a much longer blog to entry explain]).




PS> $error[0] |fl * -force



Exception             : Microsoft.PowerShell.Commands.ProcessCommandExcepti
                        on: No process with process ID 13 was found.
TargetObject          : 13
CategoryInfo          : ObjectNotFound: (13:Int32) [Stop-Process], ProcessC
                        ommandException
FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Comma
                        nds.StopProcessCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo


Switching back to the normal view can be accomplished by setting $errorview to $NULL:


 


PS> $errorview=$null
PS> stop-process 13
Stop-Process : No process with process ID 13 was found.
At line:1 char:13
+ stop-process  <<<< 13
PS>


Experiment and enjoy!


Jeffrey Snover
Windows PowerShell Architect



PSMDTAG:ERROR: ErrorView, CategoryView