Skip to content

Commit

Permalink
feat: Hints now accept byte[] as attachment (#3352)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes committed May 8, 2024
1 parent 549dc89 commit f8e84bb
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Hints now accept `byte[]` as attachment ([#3352](https://github.com/getsentry/sentry-dotnet/pull/3352))
- InApp includes/excludes can now be configured using regular expressions ([#3321](https://github.com/getsentry/sentry-dotnet/pull/3321))

### Dependencies
Expand Down
23 changes: 22 additions & 1 deletion src/Sentry/SentryHint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public SentryHint(string key, object? value)
internal void AddAttachmentsFromScope(Scope scope) => _attachments.AddRange(scope.Attachments);

/// <summary>
/// Creates a new Hint with one or more attachments.
/// Takes a path and adds the file as an attachment to the hint.
/// </summary>
/// <param name="filePath">The path to the file to attach.</param>
/// <param name="type">The type of attachment.</param>
Expand All @@ -80,6 +80,27 @@ public void AddAttachment(
}
}

/// <summary>
/// Adds a 'byte[]' as attachment to the hind.
/// </summary>
/// <param name="data">The byte array to be attached</param>
/// <param name="fileName">The filename for the attachment</param>
/// <param name="type">The type of attachment.</param>
/// <param name="contentType">The content type of the attachment.</param>
public void AddAttachment(
byte[] data,
string fileName,
AttachmentType type = AttachmentType.Default,
string? contentType = null)
{
_attachments.Add(
new SentryAttachment(
type,
new ByteAttachmentContent(data),
fileName,
contentType));
}

/// <summary>
/// Creates a new Hint with one or more attachments.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ namespace Sentry
public System.Collections.Generic.ICollection<Sentry.SentryAttachment> Attachments { get; }
public System.Collections.Generic.IDictionary<string, object?> Items { get; }
public void AddAttachment(string filePath, Sentry.AttachmentType type = 0, string? contentType = null) { }
public void AddAttachment(byte[] data, string fileName, Sentry.AttachmentType type = 0, string? contentType = null) { }
public static Sentry.SentryHint WithAttachments(params Sentry.SentryAttachment[] attachments) { }
public static Sentry.SentryHint WithAttachments(System.Collections.Generic.IEnumerable<Sentry.SentryAttachment> attachments) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ namespace Sentry
public System.Collections.Generic.ICollection<Sentry.SentryAttachment> Attachments { get; }
public System.Collections.Generic.IDictionary<string, object?> Items { get; }
public void AddAttachment(string filePath, Sentry.AttachmentType type = 0, string? contentType = null) { }
public void AddAttachment(byte[] data, string fileName, Sentry.AttachmentType type = 0, string? contentType = null) { }
public static Sentry.SentryHint WithAttachments(params Sentry.SentryAttachment[] attachments) { }
public static Sentry.SentryHint WithAttachments(System.Collections.Generic.IEnumerable<Sentry.SentryAttachment> attachments) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ namespace Sentry
public System.Collections.Generic.ICollection<Sentry.SentryAttachment> Attachments { get; }
public System.Collections.Generic.IDictionary<string, object?> Items { get; }
public void AddAttachment(string filePath, Sentry.AttachmentType type = 0, string? contentType = null) { }
public void AddAttachment(byte[] data, string fileName, Sentry.AttachmentType type = 0, string? contentType = null) { }
public static Sentry.SentryHint WithAttachments(params Sentry.SentryAttachment[] attachments) { }
public static Sentry.SentryHint WithAttachments(System.Collections.Generic.IEnumerable<Sentry.SentryAttachment> attachments) { }
}
Expand Down
1 change: 1 addition & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ namespace Sentry
public System.Collections.Generic.ICollection<Sentry.SentryAttachment> Attachments { get; }
public System.Collections.Generic.IDictionary<string, object?> Items { get; }
public void AddAttachment(string filePath, Sentry.AttachmentType type = 0, string? contentType = null) { }
public void AddAttachment(byte[] data, string fileName, Sentry.AttachmentType type = 0, string? contentType = null) { }
public static Sentry.SentryHint WithAttachments(params Sentry.SentryAttachment[] attachments) { }
public static Sentry.SentryHint WithAttachments(System.Collections.Generic.IEnumerable<Sentry.SentryAttachment> attachments) { }
}
Expand Down
20 changes: 19 additions & 1 deletion test/Sentry.Tests/HintTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public HintTests()
}

[Fact]
public void AddAttachment_AddsToHint()
public void AddAttachment_Files_AddsToHint()
{
// Arrange
var attachmentPath1 = Path.Combine(_testDirectory, Path.GetTempFileName());
Expand All @@ -29,6 +29,24 @@ public void AddAttachment_AddsToHint()
Assert.Equal(2, hint.Attachments.Count);
}

[Fact]
public void AddAttachment_ByteArray_AddsToHint()
{
// Arrange
var byteArray1 = new byte[5];
var byteArray2 = new byte[10];


var hint = new SentryHint(new SentryOptions());

// Act
hint.AddAttachment(byteArray1, "byteArray1");
hint.AddAttachment(byteArray2, "byteArray2");

// Assert
Assert.Equal(2, hint.Attachments.Count);
}

[Fact]
public void Clear_WithEntries_ClearsHintEntries()
{
Expand Down

0 comments on commit f8e84bb

Please sign in to comment.