Last active
July 19, 2022 18:40
-
-
Save ekaraman89/78cc70949d9896f40fdccf33ab586909 to your computer and use it in GitHub Desktop.
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
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