-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from wemogy/main
Release
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 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
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; | ||
} | ||
} | ||
} | ||
} |
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,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
37
src/Wemogy.Core/Json/ExceptionInformation/ExceptionInformation.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,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); | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Wemogy.Core/Json/ExceptionInformation/ExceptionInformationExtensions.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,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(); | ||
} | ||
} | ||
} |