-
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.
- Loading branch information
1 parent
f3653f7
commit 2120521
Showing
2 changed files
with
36 additions
and
4 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,36 @@ | ||
using System.Net; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
|
||
namespace Product.API.Middlewares | ||
{ | ||
public class ErrorHandlingMiddleware : IMiddleware | ||
{ | ||
|
||
private readonly ILogger _loggerService; | ||
public ErrorHandlingMiddleware(ILogger loggerService) | ||
{ | ||
_loggerService = loggerService; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext contex , RequestDelegate next) | ||
{ | ||
try | ||
{ | ||
await next(contex); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_loggerService.LogError(ex , $"An Error Captured by ErrorHandlingMiddleware : {ex.Message}"); | ||
await CreateErrorResponse(contex); | ||
} | ||
} | ||
private static async Task CreateErrorResponse( HttpContext contex) | ||
{ | ||
contex.Response.StatusCode = (int)HttpStatusCode.InternalServerError; | ||
contex.Response.ContentType = "application/json; charset=utf-8"; | ||
|
||
} | ||
|
||
} | ||
} |
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 |
---|---|---|
|
@@ -18,8 +18,4 @@ | |
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Middlewares\" /> | ||
</ItemGroup> | ||
|
||
</Project> |