safe guarding MessageIdPlugin (#12)
Protect from invalid Func value registered with the plugin Provide a meaningful stack trace when message id generator throws an exception
This commit is contained in:
Родитель
9e361d7ebf
Коммит
4966d888e3
|
@ -0,0 +1,25 @@
|
|||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
namespace Microsoft.Azure.ServiceBus.MessageId
|
||||
{
|
||||
using System;
|
||||
|
||||
internal static class Guard
|
||||
{
|
||||
public static void AgainstEmpty(string argumentName, string value)
|
||||
{
|
||||
if (value != null && string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
throw new ArgumentNullException(argumentName);
|
||||
}
|
||||
}
|
||||
public static void AgainstNull(string argumentName, object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(argumentName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,7 +29,23 @@ namespace Microsoft.Azure.ServiceBus.MessageId
|
|||
/// <param name="messageIdGenerator">Message ID generator to use.</param>
|
||||
public MessageIdPlugin(Func<Message, string> messageIdGenerator)
|
||||
{
|
||||
this.messageIdGenerator = messageIdGenerator;
|
||||
Guard.AgainstNull(nameof(messageIdGenerator), messageIdGenerator);
|
||||
this.messageIdGenerator = SafeMessageIdGenerator(messageIdGenerator);
|
||||
}
|
||||
|
||||
private Func<Message, string> SafeMessageIdGenerator(Func<Message, string> originalMessageIdGenerator)
|
||||
{
|
||||
return message =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return originalMessageIdGenerator(message);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("An exception occurred when executing message ID generator Func", exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Загрузка…
Ссылка в новой задаче