Skip to content

Commit

Permalink
Add base exception handler (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
MRmlik12 authored Apr 19, 2022
1 parent f1cc495 commit a55b3f8
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 38 deletions.
9 changes: 0 additions & 9 deletions src/Vulder.SharedKernel/BaseDomainEvent.cs

This file was deleted.

4 changes: 0 additions & 4 deletions src/Vulder.SharedKernel/BaseEntity.cs
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@ public abstract class BaseEntity
[BsonRequired]
[BsonRepresentation(BsonType.String)]
public Guid Id { get; set; }

[NotMapped]
[BsonIgnore]
public List<BaseDomainEvent> Events { get; set; } = new();
}
16 changes: 5 additions & 11 deletions src/Vulder.SharedKernel/Exceptions/AuthException.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
using System;
using System.Net;

namespace Vulder.SharedKernel.Exceptions;

public class AuthException : Exception
public class AuthException : VulderBaseException
{
public AuthException() : base()
{
}

public AuthException(string message) : base(message)
{
}

public AuthException(string message, Exception innerException) : base(message, innerException)
public override HttpStatusCode StatusCode => HttpStatusCode.Unauthorized;

public AuthException(string message = "Unable to authorize user") : base(message)
{
}
}
13 changes: 13 additions & 0 deletions src/Vulder.SharedKernel/Exceptions/VulderBaseException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Net;

namespace Vulder.SharedKernel.Exceptions;

public abstract class VulderBaseException : Exception
{
public abstract HttpStatusCode StatusCode { get; }

protected VulderBaseException(string message) : base(message)
{
}
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public async Task Invoke(HttpContext context)
{
await _next.Invoke(context);
}
catch (Exception e)
catch (VulderBaseException e)
{
LogException(e);

var response = context.Response;
response.ContentType = "application/json";
response.StatusCode = GetStatusCode(e);
response.StatusCode = (int)e.StatusCode;

var responseBody = new ExceptionModel
{
StatusCode = response.StatusCode,
StatusCode = (int)e.StatusCode,
ErrorMessage = e.Message
};

Expand All @@ -45,14 +45,5 @@ public async Task Invoke(HttpContext context)
}

private void LogException(Exception exception)
=> _logger.LogError(22, exception, "An error was thrown");

private static int GetStatusCode(Exception exception)
{
return exception switch
{
AuthException => (int)HttpStatusCode.Unauthorized,
_ => (int)HttpStatusCode.InternalServerError
};
}
=> _logger.LogError(22, exception, "An exception was thrown");
}
2 changes: 1 addition & 1 deletion src/Vulder.SharedKernel/Vulder.SharedKernel.csproj
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PackageVersion>0.1.10</PackageVersion>
<PackageVersion>0.1.11</PackageVersion>
<Title>Vulder.SharedKernel</Title>
<Authors>MRmlik12</Authors>
<Copyright>VulderApp</Copyright>
Expand Down

0 comments on commit a55b3f8

Please sign in to comment.