Skip to content

Commit

Permalink
Merge pull request #94 from wemogy/main
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
SebastianKuesters authored Jun 12, 2024
2 parents 11aaee2 + 8014546 commit a20a92f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Wemogy.Core/Errors/Extensions/ErrorTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Net;
using Wemogy.Core.Errors.Enums;

namespace Wemogy.Core.Errors.Extensions
{
public static class ErrorTypeExtensions
{
public static HttpStatusCode ToHttpStatusCode(this ErrorType errorType)
{
switch (errorType)
{
case ErrorType.Authorization:
return HttpStatusCode.Forbidden;
case ErrorType.Conflict:
return HttpStatusCode.Conflict;
case ErrorType.Failure:
return HttpStatusCode.BadRequest;
case ErrorType.NotFound:
return HttpStatusCode.NotFound;
case ErrorType.PreconditionFailed:
return HttpStatusCode.PreconditionFailed;
case ErrorType.Unexpected:
return HttpStatusCode.InternalServerError;
case ErrorType.Validation:
return HttpStatusCode.BadRequest;
default:
return HttpStatusCode.InternalServerError;
}
}
}
}
26 changes: 26 additions & 0 deletions src/Wemogy.Core/Extensions/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Net;
using FluentValidation;
using Wemogy.Core.Errors.Exceptions;
using Wemogy.Core.Errors.Extensions;

namespace Wemogy.Core.Extensions
{
public static class ExceptionExtensions
{
public static HttpStatusCode GetHttpStatusCode(this Exception exception)
{
if (exception is ErrorException errorException)
{
return errorException.ErrorType.ToHttpStatusCode();
}

if (exception is ValidationException)
{
return HttpStatusCode.BadRequest;
}

return HttpStatusCode.InternalServerError;
}
}
}
37 changes: 37 additions & 0 deletions src/Wemogy.Core/Json/ExceptionInformation/ExceptionInformation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;

namespace Wemogy.Core.Json.ExceptionInformation
{
/// <summary>
/// This class represents the most important properties of a exception in a JSON serializable way
/// Thanks to: https://stackoverflow.com/a/72968664
/// </summary>
public class ExceptionInformation
{
public string ExceptionType { get; set; }
public string Message { get; set; }
public string Source { get; set; }
public string? StackTrace { get; set; }
public ExceptionInformation? InnerException { get; set; }

public ExceptionInformation(
Exception exception,
bool includeInnerException = true,
bool includeStackTrace = false)
{
if (exception is null)
{
throw new ArgumentNullException(nameof(exception));
}

ExceptionType = exception.GetType().FullName ?? exception.GetType().Name;
Message = exception.Message;
Source = exception.Source;
StackTrace = includeStackTrace ? exception.StackTrace : null;
if (includeInnerException && exception.InnerException is not null)
{
InnerException = new ExceptionInformation(exception.InnerException, includeInnerException, includeStackTrace);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using Wemogy.Core.Extensions;

namespace Wemogy.Core.Json.ExceptionInformation
{
public static class ExceptionInformationExtensions
{
public static ExceptionInformation ToExceptionInformation(this Exception exception, bool includeInnerException = true, bool includeStackTrace = false)
{
return new ExceptionInformation(exception, includeInnerException, includeStackTrace);
}

public static string ToJson(this Exception exception, bool includeInnerException = true, bool includeStackTrace = false)
{
return exception.ToExceptionInformation(includeInnerException, includeStackTrace).ToJson();
}
}
}

0 comments on commit a20a92f

Please sign in to comment.