Skip to content

Commit

Permalink
Add null checks for delegates
Browse files Browse the repository at this point in the history
Check that actions and functions are not null and throw if they are.
  • Loading branch information
martincostello committed Dec 13, 2018
1 parent 87e11b2 commit f8a6197
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions src/JustEat.StatsD/TimerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ public static IDisposableTimer StartTimer(this IStatsDPublisher publisher, strin
/// <param name="bucket">The bucket to publish the timer for.</param>
/// <param name="action">A delegate to a method whose invocation should be timed.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="action"/> is <see langword="null"/>.
/// </exception>
public static void Time(this IStatsDPublisher publisher, string bucket, Action action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}

using (StartTimer(publisher, bucket))
{
action();
Expand All @@ -52,10 +57,15 @@ public static void Time(this IStatsDPublisher publisher, string bucket, Action a
/// <param name="bucket">The bucket to publish the timer for.</param>
/// <param name="action">A delegate to a method whose invocation should be timed.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="action"/> is <see langword="null"/>.
/// </exception>
public static void Time(this IStatsDPublisher publisher, string bucket, Action<IDisposableTimer> action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}

using (var timer = StartTimer(publisher, bucket))
{
action(timer);
Expand All @@ -73,10 +83,15 @@ public static void Time(this IStatsDPublisher publisher, string bucket, Action<I
/// A <see cref="Task"/> representing the asynchronous operation to time.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="action"/> is <see langword="null"/>.
/// </exception>
public static async Task Time(this IStatsDPublisher publisher, string bucket, Func<Task> action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}

using (StartTimer(publisher, bucket))
{
await action().ConfigureAwait(false);
Expand All @@ -95,10 +110,15 @@ public static async Task Time(this IStatsDPublisher publisher, string bucket, Fu
/// A <see cref="Task"/> representing the asynchronous operation to time.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="action"/> is <see langword="null"/>.
/// </exception>
public static async Task Time(this IStatsDPublisher publisher, string bucket, Func<IDisposableTimer, Task> action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}

using (var timer = StartTimer(publisher, bucket))
{
await action(timer).ConfigureAwait(false);
Expand All @@ -117,10 +137,15 @@ public static async Task Time(this IStatsDPublisher publisher, string bucket, Fu
/// The value from invoking <paramref name="func"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="func"/> is <see langword="null"/>.
/// </exception>
public static T Time<T>(this IStatsDPublisher publisher, string bucket, Func<T> func)
{
if (func == null)
{
throw new ArgumentNullException(nameof(func));
}

using (StartTimer(publisher, bucket))
{
return func();
Expand All @@ -139,10 +164,15 @@ public static T Time<T>(this IStatsDPublisher publisher, string bucket, Func<T>
/// The value from invoking <paramref name="func"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="func"/> is <see langword="null"/>.
/// </exception>
public static T Time<T>(this IStatsDPublisher publisher, string bucket, Func<IDisposableTimer, T> func)
{
if (func == null)
{
throw new ArgumentNullException(nameof(func));
}

using (var timer = StartTimer(publisher, bucket))
{
return func(timer);
Expand All @@ -161,10 +191,15 @@ public static T Time<T>(this IStatsDPublisher publisher, string bucket, Func<IDi
/// A <see cref="Task"/> representing the asynchronous operation to time.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="func"/> is <see langword="null"/>.
/// </exception>
public static async Task<T> Time<T>(this IStatsDPublisher publisher, string bucket, Func<Task<T>> func)
{
if (func == null)
{
throw new ArgumentNullException(nameof(func));
}

using (StartTimer(publisher, bucket))
{
return await func().ConfigureAwait(false);
Expand All @@ -183,10 +218,15 @@ public static async Task<T> Time<T>(this IStatsDPublisher publisher, string buck
/// A <see cref="Task"/> representing the asynchronous operation to time.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// <paramref name="publisher"/>, <paramref name="bucket"/> or <paramref name="func"/> is <see langword="null"/>.
/// </exception>
public static async Task<T> Time<T>(this IStatsDPublisher publisher, string bucket, Func<IDisposableTimer, Task<T>> func)
{
if (func == null)
{
throw new ArgumentNullException(nameof(func));
}

using (var timer = StartTimer(publisher, bucket))
{
return await func(timer).ConfigureAwait(false);
Expand Down

0 comments on commit f8a6197

Please sign in to comment.