Catch correct error line in PS GUI?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Catch correct error line in PS GUI?

Post by stevens »

Hi,

I have a GUI which consists of several actions (like getting ad users for textbox input etc).
Each action has a try cath built in, the catch consists of a function set-catch (below what it is doing). The set-catch is loaded in the globals.ps1

It works, BUT the line number it is stating, is wrong. F.e. when error is showing (in Powershell Studio) at line 150, the error in the gui shows line 365.
Please advise howto get the set-catch show the correct error line.

J.


---

function Set-Catch
{
$line = $_.InvocationInfo.ScriptLineNumber
Add-logs "$_.Exception.Message at line $line"
}
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Catch correct error line in PS GUI?

Post by davidc »

PowerShell Studio translates the line number to the editor line. The actual line in the generated script in your case 365, but that pertains to line 150 in the editor view of the form.
David
SAPIEN Technologies, Inc.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Catch correct error line in PS GUI?

Post by stevens »

Is there a way to make them match so I can easily get the correct line?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Catch correct error line in PS GUI?

Post by davidc »

You could export the script and run that directly (Ribbon->Deploy->Export->Export to File). But I don't recommend editing the file.
David
SAPIEN Technologies, Inc.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Catch correct error line in PS GUI?

Post by stevens »

Please clarify what you mean by that.
Export what script? Note: goal is to make the GUI show the exact PS Studio error line so when a user of the form/GUI has an error, I can find the error at the correct location.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Catch correct error line in PS GUI?

Post by stevens »

Ok, I see, export the full project to a file? Using exe right now and only for this feature using a ps1 ... isn't really worth it.
So that's the only option then?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Catch correct error line in PS GUI?

Post by davidc »

I recommend including the call stack, that way will help you determine which GUI is causing the exception. Otherwise there is no way for you to map the line to the right project file.
David
SAPIEN Technologies, Inc.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Catch correct error line in PS GUI?

Post by stevens »

Can you clarify what you mean by "call the stack" ... and how that should be done?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Catch correct error line in PS GUI?

Post by jvierra »

It is called the "call stack" and is a property of the exception object.

$_.Exception.StackTrace

https://msdn.microsoft.com/en-us/librar ... .110).aspx
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Catch correct error line in PS GUI?

Post by stevens »

Thanks!
Added it like this
$line = $_.InvocationInfo.ScriptLineNumber
$exceptionstacktrace = $_.Exception.StackTrace
Add-logs "$_.Exception.Message at line $line `n ExceptionStackTrace = $exceptionstacktrace"
Will see what it gives :-)
This topic is 6 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked