mirror of
https://github.com/techgarage-ir/MTWireGuard.git
synced 2025-08-18 16:38:19 +02:00
46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
using MTWireGuard.Models.Mikrotik;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
|
|
namespace MTWireGuard.Middlewares
|
|
{
|
|
public class ExceptionHandlerMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
//public (HttpStatusCode code, string message) GetResponse(Exception exception);
|
|
|
|
public ExceptionHandlerMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext context)
|
|
{
|
|
try
|
|
{
|
|
await _next(context);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
// log the error
|
|
//Logger.Error(exception, "error during executing {Context}", context.Request.Path.Value);
|
|
Console.WriteLine(exception);
|
|
var response = context.Response;
|
|
response.ContentType = "application/json";
|
|
|
|
// get the response code and message
|
|
//var (status, message) = GetResponse(exception);
|
|
response.StatusCode = (int)HttpStatusCode.OK;
|
|
await response.WriteAsync(exception.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class ExceptionHandlerMiddlewareExtensions
|
|
{
|
|
public static IApplicationBuilder UseExceptionHandling(this IApplicationBuilder builder)
|
|
{
|
|
return builder.UseMiddleware<ExceptionHandlerMiddleware>();
|
|
}
|
|
}
|
|
}
|