Skip to content

Commit 7bbf5ed

Browse files
author
Werner Schiller
committed
initial commit
0 parents  commit 7bbf5ed

9 files changed

+131
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bin/
2+
obj/
3+
/packages/
4+
riderModule.iml
5+
/_ReSharper.Caches/

ErrorCarrier/Error.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace ErrorCarrier;
2+
3+
public class Error : IError
4+
{
5+
public Exception? InnerException { get; }
6+
public string ErrorMessage { get; }
7+
8+
public bool IsHandled { get; private set; } = false;
9+
10+
internal Error(string errorMessage)
11+
{
12+
ErrorMessage = errorMessage;
13+
}
14+
15+
internal Error(Exception innerException)
16+
{
17+
InnerException = innerException;
18+
ErrorMessage = innerException.Message;
19+
}
20+
21+
public TOutput Handle<TOutput>(Func<Error, TOutput> handleErrorAction)
22+
{
23+
var output = handleErrorAction(this);
24+
IsHandled = true;
25+
return output;
26+
}
27+
28+
public static implicit operator Exception(Error error)
29+
{
30+
var unhandledException = error.InnerException;
31+
if (unhandledException != null)
32+
return unhandledException;
33+
return new ErrorMessageException(error.ErrorMessage);
34+
}
35+
}

ErrorCarrier/ErrorCarrier.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace ErrorCarrier;
2+
3+
public class ErrorCarrier<T> : IErrorCarrier
4+
{
5+
public ErrorCarrier(T? valueObject)
6+
{
7+
ValueObject = valueObject;
8+
}
9+
10+
public T? ValueObject { get; }
11+
12+
public static implicit operator T?(ErrorCarrier<T> carrier) => carrier.GetValue();
13+
14+
private T? GetValue()
15+
{
16+
if (Errors.All(e => e.IsHandled))
17+
return ValueObject;
18+
var unHandledErrors = Errors
19+
.Where(e => !e.IsHandled)
20+
.Cast<Error>()
21+
.Select(e => (Exception)e)
22+
.ToList();
23+
if (unHandledErrors.Count == 1)
24+
{
25+
throw unHandledErrors.Single();
26+
}
27+
28+
throw new ExceptionCollectionException(unHandledErrors);
29+
}
30+
31+
public IList<IError> Errors { get; } = new List<IError>();
32+
}

ErrorCarrier/ErrorCarrier.csproj

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

ErrorCarrier/ErrorMessageException.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ErrorCarrier;
2+
3+
public class ErrorMessageException : Exception
4+
{
5+
public ErrorMessageException(string unhandledErrorErrorMessage)
6+
: base(unhandledErrorErrorMessage)
7+
{ }
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace ErrorCarrier;
2+
3+
public class ExceptionCollectionException : Exception
4+
{
5+
public IList<Exception> InnerExceptions { get; }
6+
7+
public ExceptionCollectionException(IList<Exception> exceptions)
8+
: base($"{exceptions.Count} errors occurred:\n{string.Join(Environment.NewLine, exceptions.ToString())}")
9+
{
10+
InnerExceptions = exceptions;
11+
}
12+
}

ErrorCarrier/IError.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ErrorCarrier;
2+
3+
public interface IError
4+
{
5+
string ErrorMessage { get; }
6+
Exception? InnerException { get; }
7+
bool IsHandled { get; }
8+
}

ErrorCarrier/IErrorCarrier.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ErrorCarrier;
2+
3+
public interface IErrorCarrier
4+
{
5+
public IList<IError> Errors { get; }
6+
}

Helpers.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ErrorCarrier", "ErrorCarrier\ErrorCarrier.csproj", "{DE860415-7459-4E09-AB75-A6008A065934}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{DE860415-7459-4E09-AB75-A6008A065934}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{DE860415-7459-4E09-AB75-A6008A065934}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{DE860415-7459-4E09-AB75-A6008A065934}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{DE860415-7459-4E09-AB75-A6008A065934}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

0 commit comments

Comments
 (0)