Skip to content

Commit

Permalink
Add IExceptionHandler in shared to be used in all services
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddie4k-code committed Dec 8, 2024
1 parent 6dc86a6 commit 00e0cd1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
8 changes: 5 additions & 3 deletions shared/Class1.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace shared;
using Microsoft.AspNetCore.Diagnostics;

public class Class1
{
namespace shared;

public class Class1
{

}
26 changes: 26 additions & 0 deletions shared/ErrorHandler/CustomExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ErrorHandler;


/* Handle Exceptions in a centralized way */

public class CustomExceptionHandler : IExceptionHandler
{
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
{
var exceptionMessage = exception.Message;

var problemDetails = new ProblemDetails{
Title = "An Error has occured",
Detail = exceptionMessage
};

await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);


return true;
}
}
3 changes: 3 additions & 0 deletions shared/shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

</Project>

0 comments on commit 00e0cd1

Please sign in to comment.