Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added extension to get HttpStatusCode of a exception #93

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading