Skip to content

Add Add-SentryAttachment cmdlet with extension-based content-type defaults#134

Merged
jamescrosswell merged 9 commits into
mainfrom
mime-type-defaults
May 28, 2026
Merged

Add Add-SentryAttachment cmdlet with extension-based content-type defaults#134
jamescrosswell merged 9 commits into
mainfrom
mime-type-defaults

Conversation

@jamescrosswell

@jamescrosswell jamescrosswell commented May 27, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds an Add-SentryAttachment cmdlet that wraps Scope.AddAttachment. When no -ContentType is supplied this is infered from the file extension so that the file preview in Sentry works for common 'text' files.

Mirrors sentry-python's pattern of delegating to a default mapping when the caller doesn't specify one — adds a small explicit table for some entries not in the OS MIME database (like the PowerShell extensions).

Usage

# Before — falls back to octet-stream, UI can't preview
Edit-SentryScope { $_.AddAttachment($PSCommandPath) }

# After — content type defaults to text/plain for .ps1
Add-SentryAttachment -Path $PSCommandPath

# Bytes with a filename hint
Add-SentryAttachment -Bytes $bytes -FileName 'data.json'

# Explicit override still wins
Add-SentryAttachment -Path $file -ContentType 'text/x-powershell'

The existing $_.AddAttachment(...) path is untouched — this is purely additive.

Example

This is what we see in Sentry after running the new sample:
image

Notes

  • The broader fix (server-side extension fallback in the attachment viewer) should probably also happen in getsentry/sentry; this PR just covers the SDK-side.
  • For unknown extensions we leave the content type unset, matching the previous behaviour.

Closes #32

jamescrosswell and others added 2 commits May 28, 2026 11:58
…aults

Wraps Scope.AddAttachment and, when -ContentType is not supplied,
infers a content type from the file extension. PowerShell-specific
extensions (.ps1, .psm1, .psd1, .ps1xml, .pssc, .psrc) and common
text/structured formats (.txt, .log, .json, .xml, .csv, .yaml, ...)
get a useful default so the Sentry UI can preview them instead of
falling back to application/octet-stream.

Closes #32

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@github-actions

github-actions Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 2721fd1

jamescrosswell and others added 3 commits May 28, 2026 13:24
Demonstrates the new Add-SentryAttachment cmdlet covering all three
common paths: file by path, raw bytes with a filename hint, and an
explicit content-type override.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@jamescrosswell jamescrosswell marked this pull request as ready for review May 28, 2026 01:41

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9dba662. Configure here.

Comment thread modules/Sentry/public/Add-SentryAttachment.ps1
jamescrosswell and others added 2 commits May 28, 2026 14:03
PowerShell's $PWD and [Environment]::CurrentDirectory can diverge after
Set-Location. The Sentry SDK reads attachment files lazily at send time
via .NET I/O, which resolves relative paths against [Environment]::CurrentDirectory
— so a relative path that looks correct from the caller's perspective
could fail or attach the wrong file. Resolve eagerly with
GetUnresolvedProviderPathFromPSPath before passing the path on.

Caught by Cursor Bugbot on #134.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
The Sentry SDK may keep an attachment file handle open until Stop-Sentry
runs in AfterEach. On Windows this races with the in-test `finally`
cleanup of the temp dir, which doesn't allow concurrent access. The
temp dir is harmless to leave behind, so swallow the cleanup error
rather than fail the test.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Comment thread samples/send-attachment.ps1
jamescrosswell and others added 2 commits May 29, 2026 11:02
The Path parameter already accepts pipeline input by value; add an
example (cmdlet help + sample) and a regression test for
`$path | Add-SentryAttachment`. Byte input stays a named parameter
since piping a byte[] unrolls it into individual bytes.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Add ValueFromPipeline to -Bytes so advanced callers can pipe a byte
array (wrapped with the unary comma operator to avoid the pipeline
unrolling it). Not demonstrated in the sample since it requires
understanding the comma-operator idiom; documented in the parameter
help and covered by a test.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@jamescrosswell jamescrosswell merged commit beb6e74 into main May 28, 2026
21 checks passed
@jamescrosswell jamescrosswell deleted the mime-type-defaults branch May 28, 2026 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

attachment preview not working

2 participants