Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Sentry/Protocol/Envelopes/Envelope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,17 @@ public static Envelope FromFeedback(

if (attachments is { Count: > 0 })
{
if (attachments.Count > 1)
foreach (var attachment in attachments)
{
logger?.LogWarning("Feedback can only contain one attachment. Discarding {0} additional attachments.",
attachments.Count - 1);
}
// Safety check, in case the user forcefully added a null attachment.
if (attachment.IsNull())
{
logger?.LogWarning("Encountered a null attachment. Skipping.");
continue;
}

AddEnvelopeItemFromAttachment(items, attachments.First(), logger);
AddEnvelopeItemFromAttachment(items, attachment, logger);
}
}

if (sessionUpdate is not null)
Expand Down
45 changes: 34 additions & 11 deletions test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ public void FromFeedback_NoFeedbackContext_Throws()
}

[Fact]
public void FromFeedback_MultipleAttachments_LogsWarning()
public void FromFeedback_MultipleAttachments_AddsAll()
{
// Arrange
var feedback = new SentryFeedback(
Expand All @@ -864,23 +864,46 @@ public void FromFeedback_MultipleAttachments_LogsWarning()
Feedback = feedback
}
};
var logger = Substitute.For<IDiagnosticLogger>();
logger.IsEnabled(Arg.Any<SentryLevel>()).Returns(true);

List<SentryAttachment> attachments = [
AttachmentHelper.FakeAttachment("file1.txt"), AttachmentHelper.FakeAttachment("file2.txt")
];

// Act
using var envelope = Envelope.FromFeedback(evt, logger, attachments);
using var envelope = Envelope.FromFeedback(evt, attachments: attachments);

// Assert
envelope.Items.Count(item => item.TryGetType() == EnvelopeItem.TypeValueAttachment).Should().Be(2);
}

[Fact]
public void FromFeedback_NullAttachment_Skipped()
{
// Arrange
var feedback = new SentryFeedback(
"Everything is great!",
"foo@bar.com",
"Someone Nice",
"fake-replay-id",
"https://www.example.com",
SentryId.Create()
);
var evt = new SentryEvent
{
Level = SentryLevel.Info,
Contexts =
{
Feedback = feedback
}
};
List<SentryAttachment> attachments = [
null!, AttachmentHelper.FakeAttachment("file1.txt")
];

// Act
using var envelope = Envelope.FromFeedback(evt, attachments: attachments);

// Assert
logger.Received(1).Log(
SentryLevel.Warning,
Arg.Is<string>(m => m.Contains("Feedback can only contain one attachment")),
null,
Arg.Any<object[]>());
envelope.Items.Should().ContainSingle(item => item.TryGetType() == EnvelopeItem.TypeValueAttachment);
envelope.Items.Count(item => item.TryGetType() == EnvelopeItem.TypeValueAttachment).Should().Be(1);
}

[Fact]
Expand Down
Loading