Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System.Net;
using System.Text.Json;
namespace GlobalExceptionHandling.Middleware
{
public class GlobalExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;
public GlobalExceptionHandlingMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private async Task HandleExceptionAsync(HttpContext context, Exception exception)
{
context.Response.ContentType = "application/json";
HttpResponse response = context.Response;
ResponseModel exModel = new ResponseModel();
switch (exception)
{
case ApplicationException ex:
exModel.responseCode = (int)HttpStatusCode.BadRequest;
response.StatusCode = (int)HttpStatusCode.BadRequest;
exModel.responseMessage = "Bir hata oluştu, Lütfen bir süre sonra tekrar deneyin.";
break;
case FileNotFoundException ex:
exModel.responseCode = (int)HttpStatusCode.NotFound;
response.StatusCode = (int)HttpStatusCode.NotFound;
exModel.responseMessage = "Dosya bulunamadı";
break;
case IndexOutOfRangeException ex:
exModel.responseCode = (int)HttpStatusCode.NotFound;
response.StatusCode = (int)HttpStatusCode.NotFound;
exModel.responseMessage = "Dizi veya koleksiyonun sınırlarının dışındadır.";
break;
// .... diğer istenilen durumlar eklenebilir...
default:
exModel.responseCode = (int)HttpStatusCode.InternalServerError;
response.StatusCode = (int)HttpStatusCode.InternalServerError;
exModel.responseMessage = "Internal Server Error, Lütfen bir süre sonra tekrar deneyin.";
break;
}
string exResult = JsonSerializer.Serialize(exModel);
await context.Response.WriteAsync(exResult);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment