Skip to content

Instantly share code, notes, and snippets.

@ekaraman89
Last active July 19, 2022 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekaraman89/78cc70949d9896f40fdccf33ab586909 to your computer and use it in GitHub Desktop.
Save ekaraman89/78cc70949d9896f40fdccf33ab586909 to your computer and use it in GitHub Desktop.
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