Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit module-internal lines from stack trace when resource throws exception #38

Open
alx9r opened this issue Jan 19, 2017 · 3 comments
Open
Labels

Comments

@alx9r
Copy link
Owner

alx9r commented Jan 19, 2017

This code

$document = {
    Get-DscResource TestStub | Import-DscResource

    TestStub a @{
        Key = 'a'
        ThrowOnSet = 'always'
    }
}

Describe 'resource stack trace' {
    $h = @{}

    It 'create instructions' {
        $h.Instructions = ConfigInstructions Name $document
    }
    foreach ( $step in $h.Instructions )
    {
        It $step.Message {
            $step | Invoke-ConfigStep
        }
    }
}

outputs

Describing resource stack trace
 [+] create instructions 14.46s
 [+] Pretest: Test resource [TestStub]a 1.28s
 [-] Configure: Set resource [TestStub]a 162ms
   RuntimeException: TestStub forced exception because ThrowOnSet=always
   at Set-TargetResource<Process>, C:\Users\un1\Documents\WindowsPowerShell\Modu
les\ZeroDSC\DSCResources\TestStub\TestStub.psm1: line 56
   at Invoke-MofResourceCommand<Process>, C:\Users\un1\Documents\WindowsPowerShe
ll\Modules\ZeroDSC\Functions\mofResourceInvokerType.ps1: line 105
   at _Invoke, C:\Users\un1\Documents\WindowsPowerShell\Modules\ZeroDSC\Function
s\mofResourceInvokerType.ps1: line 21
   at Invoke-ResourceCommand<Process>, C:\Users\un1\Documents\WindowsPowerShell\
Modules\ZeroDSC\Functions\resourceInvokerType.ps1: line 88
   at Invoke, C:\Users\un1\Documents\WindowsPowerShell\Modules\ZeroDSC\Functions
\resourceInvokerType.ps1: line 33
   at Invoke, C:\Users\un1\Documents\WindowsPowerShell\Modules\ZeroDSC\Functions
\boundResourceType.ps1: line 14
   at <ScriptBlock>, C:\Users\un1\Documents\WindowsPowerShell\Modules\ZeroDSC\Fu
nctions\configInstructionsType.ps1: line 285
   at Invoke-ConfigStep<Process>, C:\Users\un1\Documents\WindowsPowerShell\Modul
es\ZeroDSC\Functions\configInstructionsType.ps1: line 335
   at <ScriptBlock>, <No file>: line 19
 [+] Configure: Test resource [TestStub]a 320ms

Most of those lines are uninformative because they are just a record of the stack unwinding inside the ZeroDSC module. They should probably be omitted from the stack trace so that Pester shows only the helpful lines.

@alx9r alx9r added the Feature label Jan 19, 2017
@alx9r
Copy link
Owner Author

alx9r commented Jan 20, 2017

Implementation ideas:

  1. Rewrite the .ScriptStackTrace property with a string that omits a bunch of the stack trace.
  2. Catch the exception at lowest point in the call stack that is inside ZeroDSC, and somehow re-throw that same exception at the highest point in the call stack.
  3. Change the way [ConfigStep] objects are set up such that the call stack between Invoke-ConfigStep and the underlying resource is minimized.

@alx9r
Copy link
Owner Author

alx9r commented Jan 20, 2017

(1) is complicated by .ScriptStackTrace being a read-only property.

try
{
    throw
}
catch
{
    $_.ScriptStackTrace = 'new trace'
}

results in

'ScriptStackTrace' is a ReadOnly property.
At C:\Users\un1\Desktop\test.ps1:7 char:5
+     $_.ScriptStackTrace = 'new trace'
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

@alx9r
Copy link
Owner Author

alx9r commented Jan 20, 2017

It doesn't seem like (2) would work because .ScriptStackTrace is populated when the exception is first thrown. So catching and re-throwing doesn't change its contents. For example,

function f1 { throw 'exception in f1' }
function f2 { try { f1 } catch { return $_ } }
function f3 { $e = f2; return $e }
function f4 { $e = f3; throw $e }

Describe 'delayed re-throw' {
    It 'test' {
        f4
    }
}

results in

Describing delayed re-throw
 [-] test 18ms
   RuntimeException: exception in f1
   at f1, C:\Users\un1\Desktop\test.ps1: line 1
   at f2, C:\Users\un1\Desktop\test.ps1: line 2
   at f3, C:\Users\un1\Desktop\test.ps1: line 3
   at f4, C:\Users\un1\Desktop\test.ps1: line 4
   at <ScriptBlock>, C:\Users\un1\Desktop\test.ps1: line 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant