Skip to content

Commit

Permalink
Fix Null Reference Exceptions (#455)
Browse files Browse the repository at this point in the history
* Fix NullReferenceException

Fix NullReferenceException when configuring publishing and subscribing if no IMessageSerialisationFactory is configured.

* Add null check for handlers

Add null check on handlers.
Specify argument name in exception.

* Add null check for handler

Add a null check on handler.
  • Loading branch information
martincostello authored and slang25 committed Nov 26, 2018
1 parent 2d09e66 commit b2ab1d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 16 additions & 1 deletion JustSaying/JustSayingFluently.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
using JustSaying.AwsTools.MessageHandling;
using JustSaying.AwsTools.QueueCreation;
using JustSaying.Extensions;
using JustSaying.Messaging.Interrogation;
using JustSaying.Messaging.MessageHandling;
using JustSaying.Messaging.MessageSerialisation;
using JustSaying.Messaging.Monitoring;
using JustSaying.Models;
using JustSaying.Messaging.Interrogation;
using Microsoft.Extensions.Logging;

namespace JustSaying
Expand Down Expand Up @@ -232,6 +232,16 @@ public IFluentSubscription IntoQueue(string queuename)
/// <returns></returns>
public IHaveFulfilledSubscriptionRequirements WithMessageHandler<T>(IHandlerAsync<T> handler) where T : Message
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}

if (_serialisationFactory == null)
{
throw new InvalidOperationException($"No {nameof(IMessageSerialisationFactory)} has been configured.");
}

// TODO - Subscription listeners should be just added once per queue,
// and not for each message handler
var thing = _subscriptionConfig.SubscriptionType == SubscriptionType.PointToPoint
Expand All @@ -250,6 +260,11 @@ public IHaveFulfilledSubscriptionRequirements WithMessageHandler<T>(IHandlerAsyn

public IHaveFulfilledSubscriptionRequirements WithMessageHandler<T>(IHandlerResolver handlerResolver) where T : Message
{
if (_serialisationFactory == null)
{
throw new InvalidOperationException($"No {nameof(IMessageSerialisationFactory)} has been configured.");
}

var thing = _subscriptionConfig.SubscriptionType == SubscriptionType.PointToPoint
? PointToPointHandler<T>()
: TopicHandler<T>();
Expand Down
7 changes: 6 additions & 1 deletion JustSaying/JustSayingFluentlyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ public static IFluentSubscription IntoDefaultQueue(this ISubscriberIntoQueue sub
public static IHaveFulfilledSubscriptionRequirements WithMessageHandlers<T>(
this IFluentSubscription sub, params IHandlerAsync<T>[] handlers) where T : Message
{
if (handlers == null)
{
throw new ArgumentNullException(nameof(handlers));
}

if (handlers.Length == 0)
{
throw new ArgumentException("No handlers in list");
throw new ArgumentException("No message handlers specified.", nameof(handlers));
}

if (handlers.Length == 1)
Expand Down

0 comments on commit b2ab1d0

Please sign in to comment.