1

In powershell 5.1, the default error response looks like this:


Aug01 14:26:01 C:\> throw [Collections.Generic.KeyNotFoundException]::new('example')
example
At line:1 char:1
+ throw [Collections.Generic.KeyNotFoundException]::new('example')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], KeyNotFoundException
    + FullyQualifiedErrorId : example

There's some good info in there. File and line, message, type name... I mean, "Fully Qualified Error ID" is redundant and bad, and telling me the CategoryInfo is "OperationStopped" is unhelpful. Plus it's ugly. But all that is negotiable.

The big sin is that I have to go to some lengths to get a decent stacktrace. When doing module development this is infuriating. I'm tired of writing $err = $error[0]; $err.ErrorRecord.ScriptStackTrace -- and yes I wrote that from memory.

Of course, when writing a script, you can work around this by wrapping the whole script in a try..catch. But what about when just using the console? I don't want to have to try..catch literally every line just to get decent error info if something goes wrong.

Now, I know that trap exists, but trap is extremely limited. Ideally I'd like some way in my $PROFILE to say "use this trap statement for every execution in the console. But that was the fist thing I tried - setting a trap in $PROFILE doesn't work because somehow traps don't follow scope even if introduced in a dot-sourced call.

2
  • Sorry @harrymc this isn't about being in a script, this is about in the console. In the prompt. I mean I can add try{}catch{Write-MyImprovedErrorHandler $_} to literally every line I type but that's not great.
    – Pxtl
    Aug 1, 2023 at 19:51
  • If you want better handling other than the default exception that is thrown, then you as the programming, must catch and throw the exception you want to see.
    – Ramhound
    Aug 1, 2023 at 20:03

1 Answer 1

1

This is the subject of the post Is it possible to customize error display in powershell?

The following is a partial quote of the accepted answer by Don Jones.

You can use the built-in $host object if all you want to do is change the text color. However, you can't change the error message itself - that's hardcoded.

What you could do is (a) suppress the error messages, and instead (b) trap the errors and display your own.

Accomplish (a) by setting $ErrorActionPreference = "SilentlyContinue" - this won't STOP the error, but it suppresses the messages.

Accomplishing (b) requires a bit more work. By default, most PowerShell commands don't produce a trappable exception. So you'll have to learn to run commands and add the -EA "Stop" parameter to generate a trappable exception if something goes wrong. Once you've done that, you can create a trap in the shell by typing:

trap {
 # handle the error here
}

You could put this in your profile script rather than typing it every time. Inside the trap, you can output whatever error text you like by using the Write-Error cmdlet.

Probably more work than you were wanting to do, but that's basically how you'd do what you asked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .