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
11 changes: 11 additions & 0 deletions .autover/changes/01370ae5-3537-4723-80b3-ee82e79e80b9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "AWS.Messaging",
"Type": "Patch",
"ChangelogMessages": [
"Update error message for handling scenario where subscriber mapping is not valid."
]
}
]
}
10 changes: 8 additions & 2 deletions src/AWS.Messaging/Serialization/EnvelopeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,14 @@ public async ValueTask<ConvertToEnvelopeResult> ConvertToEnvelopeAsync(Message s
var subscriberMapping = _messageConfiguration.GetSubscriberMapping(messageTypeIdentifier);
if (subscriberMapping is null)
{
_logger.LogError("{MessageConfiguration} does not have a valid subscriber mapping for message ID '{MessageTypeIdentifier}'", nameof(_messageConfiguration), messageTypeIdentifier);
throw new InvalidDataException($"{nameof(_messageConfiguration)} does not have a valid subscriber mapping for {nameof(messageTypeIdentifier)} '{messageTypeIdentifier}'");
var availableMappings = string.Join(", ", _messageConfiguration.SubscriberMappings.Select(m => m.MessageTypeIdentifier));
_logger.LogError("'{MessageTypeIdentifier}' is not a valid subscriber mapping. Available mappings: {AvailableMappings}",
messageTypeIdentifier,
string.IsNullOrEmpty(availableMappings) ? "none" : availableMappings);

throw new InvalidDataException(
$"'{messageTypeIdentifier}' is not a valid subscriber mapping. " +
$"Available mappings: {(string.IsNullOrEmpty(availableMappings) ? "none" : availableMappings)}");
}

var messageType = subscriberMapping.MessageType;
Expand Down
2 changes: 1 addition & 1 deletion test/AWS.Messaging.IntegrationTests/FifoSubscriberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public async Task MessagesWithoutHandlers(int numberOfMessages)
var timeElapsed = DateTime.UtcNow - processStartTime;

var inMemoryLogger = serviceProvider.GetRequiredService<InMemoryLogger>();
var errorMessages = inMemoryLogger.Logs.Where(x => x.Message.Equals("_messageConfiguration does not have a valid subscriber mapping for message ID 'AWS.Messaging.Tests.Common.Models.TransactionInfo'"));
var errorMessages = inMemoryLogger.Logs.Where(x => x.Message.StartsWith("'") && x.Message.Contains("is not a valid subscriber mapping"));
Assert.NotEmpty(errorMessages);
Assert.True(errorMessages.Count() >= numberOfMessages);
Assert.True(timeElapsed.TotalSeconds > 29);
Expand Down
2 changes: 1 addition & 1 deletion test/AWS.Messaging.IntegrationTests/SubscriberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ await publisher.PublishAsync(new ChatMessage
var timeElapsed = DateTime.UtcNow - processStartTime;

var inMemoryLogger = serviceProvider.GetRequiredService<InMemoryLogger>();
var errorMessages = inMemoryLogger.Logs.Where(x => x.Message.Equals("_messageConfiguration does not have a valid subscriber mapping for message ID 'AWS.Messaging.IntegrationTests.Models.ChatMessage'"));
var errorMessages = inMemoryLogger.Logs.Where(x => x.Message.StartsWith("'") && x.Message.Contains("is not a valid subscriber mapping"));
Assert.NotEmpty(errorMessages);
Assert.True(errorMessages.Count() >= numberOfMessages);
Assert.True(timeElapsed.TotalSeconds > 59);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -564,6 +565,50 @@
Assert.Null(exception.InnerException);
}
}

[Fact]
public async Task ConvertToEnvelope_NullSubscriberMapping_ThrowsException()
{
// ARRANGE
var serviceProvider = _serviceCollection.BuildServiceProvider();
var envelopeSerializer = serviceProvider.GetRequiredService<IEnvelopeSerializer>();
var messageEnvelope = new MessageEnvelope<AddressInfo>
{
Id = "66659d05-e4ff-462f-81c4-09e560e66a5c",
Source = new Uri("/aws/messaging", UriKind.Relative),
Version = "1.0",
MessageTypeIdentifier = "unknownMessageType", // Using an unknown message type
TimeStamp = _testdate,
Message = new AddressInfo
{
Street = "Prince St",
Unit = 123,
ZipCode = "00001"
}
};

var sqsMessage = new Message
{
Body = await envelopeSerializer.SerializeAsync(messageEnvelope),
ReceiptHandle = "receipt-handle"
};

// ACT & ASSERT
var exception = await Assert.ThrowsAsync<FailedToCreateMessageEnvelopeException>(
async () => await envelopeSerializer.ConvertToEnvelopeAsync(sqsMessage)
);

// Verify the exception message
Assert.Equal("Failed to create MessageEnvelope", exception.Message);

// Verify the inner exception type and message
Assert.IsType<InvalidDataException>(exception.InnerException);
var innerException = exception.InnerException as InvalidDataException;
Assert.Contains("'unknownMessageType' is not a valid subscriber mapping.", innerException.Message);

Check warning on line 607 in test/AWS.Messaging.UnitTests/SerializationTests/EnvelopeSerializerTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 607 in test/AWS.Messaging.UnitTests/SerializationTests/EnvelopeSerializerTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 607 in test/AWS.Messaging.UnitTests/SerializationTests/EnvelopeSerializerTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 607 in test/AWS.Messaging.UnitTests/SerializationTests/EnvelopeSerializerTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
Assert.Contains("Available mappings:", innerException.Message);
Assert.Contains("addressInfo", innerException.Message);
}

}

public class MockSerializationCallback : ISerializationCallback
Expand Down
Loading