-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes ExceptionFactory to Internals.DefaultExceptionFactory (#160)
- Loading branch information
1 parent
08d860c
commit 0273546
Showing
5 changed files
with
85 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/projects/EnsureThat/Internals/DefaultExceptionFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
src/tests/UnitTests/ExceptionFactoryTests.cs → ...UnitTests/DefaultExceptionFactoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters