Skip to content

Commit

Permalink
feat: capture errors & messages (#14)
Browse files Browse the repository at this point in the history
* error capture

* refactor

* error capture & tests

* update dependency list

* fixup deps

* fixup deps

* fixup deps

* fixup deps

* fixup

* missing deps

* test compat with windows powershell

* try to use .net framework with pwsh 6

* disable pwsh versions that don't work

* cleanup
  • Loading branch information
vaind authored Feb 13, 2024
1 parent dcb31db commit 86386d8
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ jobs:
- '7.2.0'
# - '7.1.0'
# - '7.0.0'
- '6.2.0'
- '6.1.0'
# - '6.2.0'
# - '6.1.0'
# - '6.0.0'
# And test all built-in PowerShell/Windows Powershell versions on latest CI runner images
include:
Expand Down
3 changes: 3 additions & 0 deletions dependencies/System.Buffers.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = 4.5.1
assemblyVersion = 4.0.3.0
licenseFile = LICENSE.TXT
2 changes: 2 additions & 0 deletions dependencies/System.Collections.Immutable.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = 5.0.0
licenseFile = LICENSE.TXT
3 changes: 3 additions & 0 deletions dependencies/System.Memory.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = 4.5.4
assemblyVersion = 4.0.1.1
licenseFile = LICENSE.TXT
3 changes: 3 additions & 0 deletions dependencies/System.Numerics.Vectors.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = 4.5.0
assemblyVersion = 4.1.4.0
licenseFile = LICENSE.TXT
2 changes: 2 additions & 0 deletions dependencies/System.Reflection.Metadata.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = 5.0.0
licenseFile = LICENSE.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = 4.5.3
assemblyVersion = 4.0.4.1
licenseFile = LICENSE.TXT
3 changes: 3 additions & 0 deletions dependencies/System.Text.Encodings.Web.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = 6.0.0
assemblyVersion = 6.0.0.0
licenseFile = LICENSE.TXT
3 changes: 3 additions & 0 deletions dependencies/System.ValueTuple.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = 4.5.0
assemblyVersion = 4.0.3.0
licenseFile = LICENSE.TXT
31 changes: 24 additions & 7 deletions dependencies/download.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,27 @@ Download -Dependency 'Sentry' -TFM 'net6.0'
Download -Dependency 'Sentry' -TFM 'netstandard2.0'
Download -Dependency 'Sentry' -TFM 'net462'

Download -Dependency 'System.Text.Json' -TFM 'net461' -TargetTFM 'net462'
Download -Dependency 'Microsoft.Bcl.AsyncInterfaces' -TFM 'net461' -TargetTFM 'net462'
Download -Dependency 'System.Threading.Tasks.Extensions' -TFM 'net461' -TargetTFM 'net462'

Download -Dependency 'System.Text.Json' -TFM 'netstandard2.0' -assemblyVersion '6.0.0.0'
Download -Dependency 'Microsoft.Bcl.AsyncInterfaces' -TFM 'netstandard2.0'
Download -Dependency 'System.Threading.Tasks.Extensions' -TFM 'netstandard2.0'
# You can see the list of dependencies that are actually used in sentry-dotnet/src/Sentry/bin/Debug/net462
# As for the versions in use, check the sentry package on nuget.org.
Download -TFM 'net461' -TargetTFM 'net462' -Dependency 'Microsoft.Bcl.AsyncInterfaces'
Download -TFM 'net461' -TargetTFM 'net462' -Dependency 'System.Buffers'
Download -TFM 'net461' -TargetTFM 'net462' -Dependency 'System.Collections.Immutable'
Download -TFM 'net461' -TargetTFM 'net462' -Dependency 'System.Memory'
Download -TFM 'net46' -TargetTFM 'net462' -Dependency 'System.Numerics.Vectors'
Download -TFM 'net461' -TargetTFM 'net462' -Dependency 'System.Reflection.Metadata'
Download -TFM 'net461' -TargetTFM 'net462' -Dependency 'System.Runtime.CompilerServices.Unsafe'
Download -TFM 'net461' -TargetTFM 'net462' -Dependency 'System.Text.Encodings.Web'
Download -TFM 'net461' -TargetTFM 'net462' -Dependency 'System.Text.Json'
Download -TFM 'net461' -TargetTFM 'net462' -Dependency 'System.Threading.Tasks.Extensions'
Download -TFM 'net461' -TargetTFM 'net462' -Dependency 'System.ValueTuple'

Download -TFM 'netstandard2.0' -Dependency 'Microsoft.Bcl.AsyncInterfaces'
Download -TFM 'netstandard2.0' -Dependency 'System.Buffers'
Download -TFM 'netstandard2.0' -Dependency 'System.Collections.Immutable'
Download -TFM 'netstandard2.0' -Dependency 'System.Memory'
Download -TFM 'netstandard2.0' -Dependency 'System.Numerics.Vectors'
Download -TFM 'netstandard2.0' -Dependency 'System.Reflection.Metadata'
Download -TFM 'netstandard2.0' -Dependency 'System.Runtime.CompilerServices.Unsafe'
Download -TFM 'netstandard2.0' -Dependency 'System.Text.Encodings.Web'
Download -TFM 'netstandard2.0' -Dependency 'System.Text.Json' -assemblyVersion '6.0.0.0'
Download -TFM 'netstandard2.0' -Dependency 'System.Threading.Tasks.Extensions'
6 changes: 3 additions & 3 deletions module/Sentry.psd1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# https://learn.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-module-manifest
@{
# Script module or binary module file associated with this manifest.
# RootModule = ''
RootModule = 'Sentry.psm1'

# Version number of this module.
ModuleVersion = '0.0.1'
Expand Down Expand Up @@ -31,13 +31,13 @@
ScriptsToProcess = @('assemblies-loader.ps1')

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @()
FunctionsToExport = @('Out-Sentry', 'Invoke-WithSentry')

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()

# Variables to export from this module
VariablesToExport = '*'
VariablesToExport = @()

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()
Expand Down
53 changes: 53 additions & 0 deletions module/Sentry.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function Out-Sentry
{
param(
[Parameter(ValueFromPipeline = $true)]
[System.Management.Automation.ErrorRecord]
$ErrorRecord,

[Parameter(ValueFromPipeline = $true)]
[System.Exception]
$Exception,

[Parameter(ValueFromPipeline = $true)]
[string]
$Message
)

begin {}
process
{
if ($ErrorRecord -ne $null)
{
# TODO details
[Sentry.SentrySdk]::CaptureException($ErrorRecord.Exception)
}
if ($Exception -ne $null -and ($Message -eq $null -or "$Exception" -eq "$Message"))
{
[Sentry.SentrySdk]::CaptureException($Exception)
}
if ($Message -ne $null -and $Message -ne $ErrorRecord -and $Message -ne $Exception)
{
[Sentry.SentrySdk]::CaptureMessage($Message)
}
}
end {}
}

function Invoke-WithSentry
{
param(
[scriptblock]
$ScriptBlock
)

try
{
& $ScriptBlock
}
catch
{
$_ | Out-Sentry
throw
}
}
80 changes: 80 additions & 0 deletions tests/capture.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
BeforeAll {
$events = [System.Collections.Generic.List[Sentry.SentryEvent]]::new();
$options = [Sentry.SentryOptions]::new()
$options.Debug = $true
$options.Dsn = 'https://[email protected]/1'
$options.AutoSessionTracking = $false
$options.SetBeforeSend([System.Func[Sentry.SentryEvent, Sentry.SentryEvent]] {
param([Sentry.SentryEvent]$e)
$events.Add($e)
return $null # Prevent sending
});
[Sentry.SentrySdk]::init($options)
}

AfterAll {
[Sentry.SentrySdk]::Close()
}


Describe 'Out-Sentry' {
AfterEach {
$events.Clear()
}

It 'captures message' {
'message' | Out-Sentry
$events.Count | Should -Be 1
[Sentry.SentryEvent]$event = $events.ToArray()[0]
$event.Exception | Should -Be $null
$event.Message.Message | Should -Be 'message'
}

It 'captures error record' {
try
{
throw 'error'
}
catch
{
$_ | Out-Sentry
}
$events.Count | Should -Be 1
[Sentry.SentryEvent]$event = $events.ToArray()[0]
$event.Exception | Should -BeOfType [System.Management.Automation.RuntimeException]
$event.Exception.Message | Should -Be 'error'
}

It 'captures exception' {
try
{
throw 'exception'
}
catch
{
$_.Exception | Out-Sentry
}
$events.Count | Should -Be 1
[Sentry.SentryEvent]$event = $events.ToArray()[0]
$event.Exception | Should -BeOfType [System.Management.Automation.RuntimeException]
$event.Exception.Message | Should -Be 'exception'
}
}

Describe 'Invoke-WithSentry' {
AfterEach {
$events.Clear()
}

It 'captures error record' {
try
{
Invoke-WithSentry { throw 'inside invoke' }
}
catch {}
$events.Count | Should -Be 1
[Sentry.SentryEvent]$event = $events.ToArray()[0]
$event.Exception | Should -BeOfType [System.Management.Automation.RuntimeException]
$event.Exception.Message | Should -Be 'inside invoke'
}
}
13 changes: 4 additions & 9 deletions tests/init.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
BeforeAll {
. $PSScriptRoot/utils.ps1
}

Describe 'SentrySdk' {
AfterEach {
[Sentry.SentrySdk]::close()
Expand All @@ -23,15 +27,6 @@ Describe 'SentrySdk' {

# Currently breaks tests, see 'https://github.com/getsentry/sentry-dotnet/issues/3141'
# It 'init respects options' {
# class TestLogger:Sentry.Infrastructure.DiagnosticLogger
# {
# TestLogger([Sentry.SentryLevel]$level) : base($level) {}

# $entries = [System.Collections.Concurrent.ConcurrentQueue[string]]::new();

# [void]LogMessage([string] $message) { $this.entries.Enqueue($message); }
# }

# $logger = [TestLogger]::new([Sentry.SentryLevel]::Debug)

# $options = [Sentry.SentryOptions]::new()
Expand Down
19 changes: 19 additions & 0 deletions tests/utils.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class RecordingTransport:Sentry.Extensibility.ITransport
{
$envelopes = [System.Collections.Concurrent.ConcurrentQueue[Sentry.Protocol.Envelopes.Envelope]]::new();

[System.Threading.Tasks.Task]SendEnvelopeAsync([Sentry.Protocol.Envelopes.Envelope] $envelope, [System.Threading.CancellationToken] $cancellationToken)
{
$this.envelopes.Enqueue($envelope);
return [System.Threading.Tasks.Task]::CompletedTask;
}
}

class TestLogger:Sentry.Infrastructure.DiagnosticLogger
{
TestLogger([Sentry.SentryLevel]$level) : base($level) {}

$entries = [System.Collections.Concurrent.ConcurrentQueue[string]]::new();

[void]LogMessage([string] $message) { $this.entries.Enqueue($message); }
}

0 comments on commit 86386d8

Please sign in to comment.