Skip to content

Commit

Permalink
fix: improved ErrorInformationExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianKuesters committed Jul 10, 2024
1 parent 8014546 commit 6c7979e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/Wemogy.Core/Errors/Exceptions/ErrorException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public abstract class ErrorException : Exception
{
public ErrorType ErrorType { get; set; }
public string Code { get; set; }

public string Description { get; set; }

protected ErrorException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Wemogy.Core.Errors.Enums;

namespace Wemogy.Core.Json.ExceptionInformation
{
public class ErrorExceptionInformation
{
public ErrorType ErrorType { get; set; }
public string Code { get; set; }
public string Description { get; set; }

public ErrorExceptionInformation(
ErrorType errorType,
string code,
string description)
{
ErrorType = errorType;
Code = code;
Description = description;
}
}
}
18 changes: 18 additions & 0 deletions src/Wemogy.Core/Json/ExceptionInformation/ExceptionInformation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Wemogy.Core.Errors.Exceptions;

namespace Wemogy.Core.Json.ExceptionInformation
{
Expand All @@ -14,6 +15,8 @@ public class ExceptionInformation
public string? StackTrace { get; set; }
public ExceptionInformation? InnerException { get; set; }

public ErrorExceptionInformation? ErrorExceptionInformation { get; set; }

public ExceptionInformation(
Exception exception,
bool includeInnerException = true,
Expand All @@ -32,6 +35,21 @@ public ExceptionInformation(
{
InnerException = new ExceptionInformation(exception.InnerException, includeInnerException, includeStackTrace);
}

if (exception is ErrorException errorException)
{
ErrorExceptionInformation = new ErrorExceptionInformation(
errorException.ErrorType,
errorException.Code,
errorException.Description);
}
}

public ExceptionInformation()
{
ExceptionType = string.Empty;
Message = string.Empty;
Source = string.Empty;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Wemogy.Core.Errors;
using Wemogy.Core.Errors.Enums;
using Wemogy.Core.Extensions;

namespace Wemogy.Core.Json.ExceptionInformation
Expand All @@ -14,5 +16,34 @@ public static string ToJson(this Exception exception, bool includeInnerException
{
return exception.ToExceptionInformation(includeInnerException, includeStackTrace).ToJson();
}

public static Exception ToException(this ExceptionInformation exceptionInformation)
{
// destructure
var errorExceptionInformation = exceptionInformation.ErrorExceptionInformation;

if (errorExceptionInformation != null)
{
switch (errorExceptionInformation.ErrorType)
{
case ErrorType.Failure:
return Error.Failure(errorExceptionInformation.Code, errorExceptionInformation.Description);
case ErrorType.Unexpected:
return Error.Unexpected(errorExceptionInformation.Code, errorExceptionInformation.Description);
case ErrorType.Validation:
return Error.Validation(errorExceptionInformation.Code, errorExceptionInformation.Description);
case ErrorType.Conflict:
return Error.Conflict(errorExceptionInformation.Code, errorExceptionInformation.Description);
case ErrorType.NotFound:
return Error.NotFound(errorExceptionInformation.Code, errorExceptionInformation.Description);
case ErrorType.Authorization:
return Error.Authorization(errorExceptionInformation.Code, errorExceptionInformation.Description);
case ErrorType.PreconditionFailed:
return Error.PreconditionFailed(errorExceptionInformation.Code, errorExceptionInformation.Description);
}
}

return new Exception(exceptionInformation.Message);
}
}
}

0 comments on commit 6c7979e

Please sign in to comment.