Skip to content

Commit

Permalink
Changes ExceptionFactory to Internals.DefaultExceptionFactory (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwertheim authored Jul 6, 2021
1 parent 08d860c commit 0273546
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/projects/EnsureThat/Ensure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class Ensure
/// <summary>
/// Gets or Sets the Exception factory to use.
/// </summary>
public static IExceptionFactory ExceptionFactory { get; set; } = new ExceptionFactory();
public static IExceptionFactory ExceptionFactory { get; set; } = EnsureThat.ExceptionFactory.Default;

/// <summary>
/// Ensures for objects.
Expand Down
9 changes: 9 additions & 0 deletions src/projects/EnsureThat/ExceptionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using EnsureThat.Internals;

namespace EnsureThat
{
public static class ExceptionFactory
{
public static IExceptionFactory Default { get; } = new DefaultExceptionFactory();
}
}
67 changes: 0 additions & 67 deletions src/projects/EnsureThat/IExceptionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using JetBrains.Annotations;
using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;

namespace EnsureThat
{
Expand All @@ -10,70 +9,4 @@ public interface IExceptionFactory
Exception ArgumentNullException([NotNull] string defaultMessage, string paramName, OptsFn optsFn = null);
Exception ArgumentOutOfRangeException<TValue>([NotNull] string defaultMessage, string paramName, TValue value, OptsFn optsFn = null);
}

public sealed class ExceptionFactory : IExceptionFactory
{
[return: NotNull]
[Pure]
public Exception ArgumentException(string defaultMessage, string paramName, OptsFn optsFn = null)
{
if (optsFn != null)
{
var opts = optsFn(new EnsureOptions());

if (opts.CustomExceptionFactory != null)
return opts.CustomExceptionFactory(defaultMessage, paramName);

if (opts.CustomException != null)
return opts.CustomException;

if (opts.CustomMessage != null)
return new ArgumentException(opts.CustomMessage, paramName);
}

return new ArgumentException(defaultMessage, paramName);
}

[return: NotNull]
[Pure]
public Exception ArgumentNullException(string defaultMessage, string paramName, OptsFn optsFn = null)
{
if (optsFn != null)
{
var opts = optsFn(new EnsureOptions());

if (opts.CustomExceptionFactory != null)
return opts.CustomExceptionFactory(defaultMessage, paramName);

if (opts.CustomException != null)
return opts.CustomException;

if (opts.CustomMessage != null)
return new ArgumentNullException(paramName, opts.CustomMessage);
}

return new ArgumentNullException(paramName, defaultMessage);
}

[return: NotNull]
[Pure]
public Exception ArgumentOutOfRangeException<TValue>(string defaultMessage, string paramName, TValue value, OptsFn optsFn = null)
{
if (optsFn != null)
{
var opts = optsFn(new EnsureOptions());

if (opts.CustomExceptionFactory != null)
return opts.CustomExceptionFactory(defaultMessage, paramName);

if (opts.CustomException != null)
return opts.CustomException;

if (opts.CustomMessage != null)
return new ArgumentOutOfRangeException(paramName, value, opts.CustomMessage);
}

return new ArgumentOutOfRangeException(paramName, value, defaultMessage);
}
}
}
72 changes: 72 additions & 0 deletions src/projects/EnsureThat/Internals/DefaultExceptionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using JetBrains.Annotations;
using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;

namespace EnsureThat.Internals
{
public sealed class DefaultExceptionFactory : IExceptionFactory
{
[return: NotNull]
[Pure]
public Exception ArgumentException(string defaultMessage, string paramName, OptsFn optsFn = null)
{
if (optsFn != null)
{
var opts = optsFn(new EnsureOptions());

if (opts.CustomExceptionFactory != null)
return opts.CustomExceptionFactory(defaultMessage, paramName);

if (opts.CustomException != null)
return opts.CustomException;

if (opts.CustomMessage != null)
return new ArgumentException(opts.CustomMessage, paramName);
}

return new ArgumentException(defaultMessage, paramName);
}

[return: NotNull]
[Pure]
public Exception ArgumentNullException(string defaultMessage, string paramName, OptsFn optsFn = null)
{
if (optsFn != null)
{
var opts = optsFn(new EnsureOptions());

if (opts.CustomExceptionFactory != null)
return opts.CustomExceptionFactory(defaultMessage, paramName);

if (opts.CustomException != null)
return opts.CustomException;

if (opts.CustomMessage != null)
return new ArgumentNullException(paramName, opts.CustomMessage);
}

return new ArgumentNullException(paramName, defaultMessage);
}

[return: NotNull]
[Pure]
public Exception ArgumentOutOfRangeException<TValue>(string defaultMessage, string paramName, TValue value, OptsFn optsFn = null)
{
if (optsFn != null)
{
var opts = optsFn(new EnsureOptions());

if (opts.CustomExceptionFactory != null)
return opts.CustomExceptionFactory(defaultMessage, paramName);

if (opts.CustomException != null)
return opts.CustomException;

if (opts.CustomMessage != null)
return new ArgumentOutOfRangeException(paramName, value, opts.CustomMessage);
}

return new ArgumentOutOfRangeException(paramName, value, defaultMessage);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using System;
using System.Collections.Generic;
using EnsureThat;
using EnsureThat.Internals;
using FluentAssertions;
using Xunit;

namespace UnitTests
{
public class ExceptionFactoryTests : UnitTestBase
public class DefaultExceptionFactoryTests : UnitTestBase
{
private const string DefaultMessage = "54a170d6997c400487c177768f72aa7a";
private const string CustomMessage = "3b3e6082d4c1482fbc775abc11a2e415";
private const string DummyValue = "61927e885db34e08b9a7211c7eb74c36";
private readonly Exception _exceptionFactoryException = new KeyNotFoundException();
private readonly Exception _customException = new("036859e6693848b199575feffe17bd46");
private readonly ExceptionFactory _sut = new();
private readonly DefaultExceptionFactory _sut = new();

[Fact]
public void ArgumentException_UsesDefaults_WhenNoOptionsAreProvided()
Expand Down

0 comments on commit 0273546

Please sign in to comment.