$ERRORVIEW="CATEGORYVIEW"

$ERRORVIEW="CATEGORYVIEW"

Rate This
  • Comments 3

<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

Leave a Comment
  • Please add 4 and 1 and type the answer here:
  • Post
  • That is very helpful. I found that the errors were hard to parse behind the barrage of <<<'s

    This makes it much more readable and look more java-like
  • It is extremely helpful, and would be even more so if this CategoryView provided relevant details for all types of errors. See these examples where CategoryView does not help:

    EXAMPLE 1 -

    ~[2/8]> gwmi -class Win32_Service -where "StartMode='Auto'"
    Get-WmiObject : A parameter cannot be found that matches parameter name 'where'.
    At line:1 char:42
    + get-wmiobject -class Win32_Service -where  <<<< "StartMode='Auto'"
    ~[2/8]> $errorview="CategoryView"
    ~[2/8]> gwmi -class Win32_Service -where "StartMode='Auto'"
    InvalidArgument: (:) [Get-WmiObject], ParameterBindingException

    In the above example, normal view is verbose but very explicit, whereas the CategoryView does not specify the name of the invalid argument.

    EXAMPLE 2 -

    ~[2/8]> 10/0
    Attempted to divide by zero.
    At line:1 char:4
    + 10/0 <<<<
    ~[2/8]> $errorview="CategoryView"
    ~[2/8]> 10/0
    NotSpecified: (:) [], ParentContainsErrorRecordException

    In the above example, if I was only relying on the CategoryView message I would have absolutely no idea of what is going on (of course, this is just an example to illustrate the lack of clarity of CategoryView as everyone will have already noticed I can't divide by zero).

    Are there plans to increase the relevance of error messages in this CategoryView?

    Thanks,
    Jacques
  • I just set the value of $ErrorView to a random charater, say "a" and $ErrorView would still work just like "NormalView" .

    [^_^]PS[307]>$ErrorView = "a"
    [^_^]PS[308]>$ErrorView
    a
    [^_^]PS[309]>a
    'a' is not recognized as a cmdlet, function, operable program, or script file.
    At line:1 char:1
    + a <<<<

    Is this intended by design? or a bug?
Page 1 of 1 (3 items)