Skip to content

Commit

Permalink
Adding the exception filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Keilholz committed Jan 31, 2024
1 parent 29e8850 commit 2ab8d48
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/Wam.Core/Configuration/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static IServiceCollection AddWamCoreConfiguration(
}

services.AddHexMasterCache(configuration);
services.AddExceptionHandler<WamExceptionHandler>();
return services;
}
}
2 changes: 1 addition & 1 deletion src/Wam.Core/Exceptions/WamException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Wam.Core.Exceptions;

public class WamException(WamErrorCode error, string message) : Exception(message)
public abstract class WamException(WamErrorCode error, string message) : Exception(message)
{
public WamErrorCode Error { get; } = error;
}
30 changes: 0 additions & 30 deletions src/Wam.Core/Exceptions/WamExceptionHandler.cs

This file was deleted.

26 changes: 26 additions & 0 deletions src/Wam.Core/Filters/WamExceptionFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Wam.Core.DataTransferObjects;
using Wam.Core.Exceptions;

namespace Wam.Core.Filters;

public class WamExceptionFilter : IActionFilter, IOrderedFilter
{
public int Order { get; } = int.MaxValue - 10;

public void OnActionExecuting(ActionExecutingContext context) { }

public void OnActionExecuted(ActionExecutedContext context)
{
if (context.Exception is WamException exception)
{
context.Result = new ObjectResult(
new ExceptionDto(exception.Error.Code, exception.Error.TranslationKey.ToLower(), exception.Message))
{
StatusCode = 409
};
context.ExceptionHandled = true;
}
}
}

0 comments on commit 2ab8d48

Please sign in to comment.