Skip to content

Commit 2cf1225

Browse files
authored
fix: capture doesn't work if debug=false (#74)
* fix: capture doesn't work if debug=false * use script scope * chore: changelog * fix stop-sentry * fix ci * fix ci * use diagnostic logger for debug logging * silently skip if diagnostic logger is missing
1 parent ff7d911 commit 2cf1225

File tree

8 files changed

+35
-6
lines changed

8 files changed

+35
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixes
66

7+
- Capture doesn't work if `-Debug` wasn't set ([#74](https://github.com/getsentry/sentry-powershell/pull/74))
78
- Don't log debug messages if `-Debug` wasn't set ([#75](https://github.com/getsentry/sentry-powershell/pull/75))
89

910
### Dependencies

modules/Sentry/private/DiagnosticLogger.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class DiagnosticLogger : Sentry.Extensibility.IDiagnosticLogger
5555
# see https://github.com/PowerShell/PowerShell/issues/5148
5656
if ($global:PSVersionTable.PSEdition -eq 'Desktop') {
5757
$pref = Get-Variable DebugPreference
58-
if ($pref.value -eq "Inquire") {
58+
if (($null -ne $pref) -and ($pref.value -eq "Inquire")) {
5959
$DebugPreference = 'Continue'
6060
}
6161
}

modules/Sentry/private/StackTraceProcessor.ps1

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ class StackTraceProcessor : SentryEventProcessor
44
[System.Management.Automation.InvocationInfo]$InvocationInfo
55
[System.Management.Automation.CallStackFrame[]]$StackTraceFrames
66
[string[]]$StackTraceString
7+
hidden [Sentry.Extensibility.IDiagnosticLogger] $logger
78
hidden [string[]] $modulePaths
89
hidden [hashtable] $pwshModules = @{}
910

10-
StackTraceProcessor()
11+
StackTraceProcessor([Sentry.SentryOptions] $options)
1112
{
13+
$this.logger = $options.DiagnosticLogger
14+
if ($null -eq $this.logger) {
15+
$this.logger = Get-Variable -Scope script -Name SentryPowerShellDiagnosticLogger -ValueOnly -ErrorAction SilentlyContinue
16+
}
17+
1218
if ($env:PSModulePath.Contains(';'))
1319
{
1420
# Windows
@@ -369,7 +375,13 @@ class StackTraceProcessor : SentryEventProcessor
369375
{
370376
if ($lines.Count -lt $sentryFrame.LineNumber)
371377
{
372-
Write-Debug "Couldn't set frame context because the line number ($($sentryFrame.LineNumber)) is lower than the available number of source code lines ($($lines.Count))."
378+
if ($null -ne $this.logger) {
379+
$this.logger.Log(
380+
[Sentry.SentryLevel]::Debug,
381+
"Couldn't set frame context because the line number ($($sentryFrame.LineNumber)) " +
382+
"is lower than the available number of source code lines ($($lines.Count))."
383+
)
384+
}
373385
return
374386
}
375387

modules/Sentry/private/SynchronousTransport.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class SynchronousTransport : Sentry.Http.HttpTransportBase, Sentry.Extensibility
1111
SynchronousTransport([Sentry.SentryOptions] $options) : base($options)
1212
{
1313
$this.logger = $options.DiagnosticLogger
14+
if ($null -eq $this.logger) {
15+
$this.logger = Get-Variable -Scope script -Name SentryPowerShellDiagnosticLogger -ValueOnly -ErrorAction SilentlyContinue
16+
}
1417

1518
# These are internal methods, so we need to use reflection to access them.
1619
$instanceMethod = [System.Reflection.BindingFlags]::Instance + [System.Reflection.BindingFlags]::NonPublic + [System.Reflection.BindingFlags]::Public;
@@ -43,7 +46,9 @@ class SynchronousTransport : Sentry.Http.HttpTransportBase, Sentry.Extensibility
4346
$content = $reader.ReadToEnd()
4447
$reader.Close()
4548

46-
$this.logger.Log([Sentry.SentryLevel]::Debug, 'Sending content synchronously, Content-Length: {0}', $null, $content.Length)
49+
if ($null -ne $this.logger) {
50+
$this.logger.Log([Sentry.SentryLevel]::Debug, 'Sending content synchronously, Content-Length: {0}', $null, $content.Length)
51+
}
4752

4853
$ProgressPreference = 'SilentlyContinue'
4954
$psResponse = Invoke-WebRequest -Uri $request.RequestUri -Method $request.Method.Method -Headers $headers -Body $content -UseBasicParsing

modules/Sentry/public/Out-Sentry.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ function Out-Sentry
2929
{
3030
if (-not [Sentry.SentrySdk]::IsEnabled)
3131
{
32+
# Workaround for:
33+
# NullReferenceException: Object reference not set to an instance of an object.
34+
# at Out-Sentry<Process>, D:\a\sentry-powershell\sentry-powershell\modules\Sentry\public\Out-Sentry.ps1:32
35+
try {
36+
Write-Debug "Sentry is not started: Out-Sentry invocation ignored."
37+
} catch {}
3238
return
3339
}
3440

3541
$options = Get-CurrentOptions
3642
[Sentry.SentryEvent]$event_ = $null
37-
$processor = [StackTraceProcessor]::new()
43+
$processor = [StackTraceProcessor]::new($options)
3844

3945
if ($ErrorRecord -ne $null)
4046
{

modules/Sentry/public/Start-Sentry.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ function Start-Sentry
4747
}
4848

4949
$logger = [DiagnosticLogger]::new($options.DiagnosticLevel)
50+
51+
# Note: this is currently a no-op if options.debug == false; see https://github.com/getsentry/sentry-dotnet/issues/3212
52+
# As a workaround, we set the logger as a global variable so that we can reach it in other scripts.
5053
$options.DiagnosticLogger = $logger
54+
$script:SentryPowerShellDiagnosticLogger = $logger
5155

5256
if ($null -eq $options.Transport)
5357
{

modules/Sentry/public/Stop-Sentry.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
function Stop-Sentry
22
{
33
[Sentry.SentrySdk]::Close()
4+
Remove-Variable -Scope script -Name SentryPowerShellDiagnosticLogger -ErrorAction SilentlyContinue
45
}

tests/stacktrace-processor.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Describe 'StackTraceProcessor' {
88
$event_.Message = 'Test'
99
$event_.Level = [Sentry.SentryLevel]::Info
1010

11-
$sut = [StackTraceProcessor]::new()
11+
$sut = [StackTraceProcessor]::new([Sentry.SentryOptions]::new())
1212
$sut.StackTraceString = 'at funcB, C:\dev\sentry-powershell\tests\throwing.ps1: line 17
1313
at <ScriptBlock>, <No file>: line 1
1414
at <ScriptBlock>, : line 3' -split "[`r`n]+"

0 commit comments

Comments
 (0)