-
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.
feat: Implementing error handling middleware
- Loading branch information
1 parent
da79a7c
commit 0700241
Showing
2 changed files
with
85 additions
and
7 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
87 changes: 81 additions & 6 deletions
87
src/shared/Example.EventDriven.DependencyInjection/Middlewares/ErrorHandlingMiddleware.cs
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 |
---|---|---|
@@ -1,13 +1,88 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Example.EventDriven.Domain.Exceptions; | ||
using Example.EventDriven.Domain.Gateways.Logger; | ||
using FluentValidation; | ||
using Microsoft.AspNetCore.Http; | ||
using Newtonsoft.Json; | ||
using System.Net; | ||
|
||
namespace Example.EventDriven.DependencyInjection.Middlewares | ||
{ | ||
public class ErrorHandlingMiddleware | ||
public sealed class ErrorHandlingMiddleware | ||
{ | ||
private readonly ILoggerManager _logger; | ||
private readonly RequestDelegate _next; | ||
|
||
public ErrorHandlingMiddleware(ILoggerManager logger, RequestDelegate next) | ||
{ | ||
_logger = logger; | ||
_next = next; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
try | ||
{ | ||
await _next(context); | ||
} | ||
catch (BusinessException ex) | ||
{ | ||
_logger.Log("Business error caught by middleware", LoggerManagerSeverity.WARNING, ("exception", ex)); | ||
|
||
await HandleExceptionAsync(context, ex); | ||
} | ||
catch (ValidationException ex) | ||
{ | ||
_logger.Log("Validation error caught by middleware", LoggerManagerSeverity.WARNING, ("exception", ex)); | ||
|
||
await HandleExceptionAsync(context, ex); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogException("Unexpected error caught by middleware", LoggerManagerSeverity.ERROR, ex); | ||
|
||
await HandleExceptionAsync(context); | ||
} | ||
} | ||
|
||
private static Task HandleExceptionAsync(HttpContext context, BusinessException exception) | ||
{ | ||
var code = HttpStatusCode.BadRequest; | ||
|
||
var result = JsonConvert.SerializeObject(new { Errors = new string[1] { exception.Message } }); | ||
|
||
return ErrorResponse(context, result, code); | ||
} | ||
|
||
private static Task HandleExceptionAsync(HttpContext context, ValidationException exception) | ||
{ | ||
var code = HttpStatusCode.BadRequest; | ||
|
||
var result = JsonConvert.SerializeObject(new { Errors = exception.Errors.Select(e => e.ErrorMessage).ToArray() }); | ||
|
||
return ErrorResponse(context, result, code); | ||
} | ||
|
||
private static Task HandleExceptionAsync(HttpContext context) | ||
{ | ||
var code = HttpStatusCode.InternalServerError; | ||
|
||
return ErrorResponse(context, code); | ||
} | ||
|
||
private static Task ErrorResponse(HttpContext context, HttpStatusCode code) | ||
{ | ||
var result = JsonConvert.SerializeObject(new { Errors = new string[1] { "UnexpectedError" } }); | ||
|
||
return ErrorResponse(context, result, code); | ||
} | ||
|
||
private static Task ErrorResponse(HttpContext context, string result, HttpStatusCode code) | ||
{ | ||
context.Response.ContentType = "application/json"; | ||
|
||
context.Response.StatusCode = (int)code; | ||
|
||
return context.Response.WriteAsync(result); | ||
} | ||
} | ||
} |