Skip to content

Commit

Permalink
删除中间件使用过滤器try
Browse files Browse the repository at this point in the history
  • Loading branch information
239573049 committed Dec 20, 2024
1 parent 2024c07 commit 68579e9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 92 deletions.
8 changes: 5 additions & 3 deletions src/NuGet.Next/Apis/ApiExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using NuGet.Next.Core;
using NuGet.Next.Filter;
using NuGet.Next.Options;
using NuGet.Next.Protocol.Models;

Expand All @@ -11,7 +12,8 @@ public static IEndpointRouteBuilder MapApis(this IEndpointRouteBuilder app)
{
var options = app.ServiceProvider.GetRequiredService<NuGetNextOptions>();

var group = app.MapGroup(options.PathBase ?? "/");
var group = app.MapGroup(options.PathBase ?? "/")
.AddEndpointFilter<ExceptionFilter>();


group.Map("/v3/package/{id}/index.json",
Expand Down Expand Up @@ -194,14 +196,14 @@ await apis.DeleteAsync(id))
async ([FromServices] UserKeyApis apis, string id) =>
await apis.EnableAsync(id))
.WithOpenApi();

var panel = group.MapGroup("api/v3/panel");

panel.MapGet(string.Empty,
async ([FromServices] PanelApi apis) =>
await apis.GetAsync())
.WithOpenApi();

return app;
}
}
74 changes: 74 additions & 0 deletions src/NuGet.Next/Filter/ExceptionFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using NuGet.Next.Core.Exceptions;
using NuGet.Next.Protocol.Models;

namespace NuGet.Next.Filter;

public class ExceptionFilter(ILogger<ExceptionFilter> logger) : IEndpointFilter
{
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
try
{
return await next(context);
}
catch (NotFoundException notFoundException)
{
context.HttpContext.Response.StatusCode = 404;
context.HttpContext.Response.ContentType = "application/json";

logger.LogWarning("Resource not found: {Path}", context.HttpContext.Request.Path);
return new OkResponse(false, notFoundException.Message);
}
catch (UnauthorizedAccessException)
{
context.HttpContext.Response.StatusCode = 401;
context.HttpContext.Response.ContentType = "application/json";

var response = new OkResponse(false, "未授权的访问");

logger.LogWarning("Unauthorized access to {Path}", context.HttpContext.Request.Path);

return response;
}
catch (Exception ex)
{
switch (ex)
{
case InvalidOperationException or BadRequestException:
{
context.HttpContext.Response.StatusCode = 200;
context.HttpContext.Response.ContentType = "application/json";

var response = new OkResponse(false, ex.Message);

logger.LogWarning("Invalid operation: {Message}", ex.Message);

return response;
}
case ForbiddenException:
{
context.HttpContext.Response.StatusCode = 403;
context.HttpContext.Response.ContentType = "application/json";

var response = new OkResponse(false, ex.Message);


logger.LogWarning("Forbidden access to {Path}", context.HttpContext.Request.Path);

return response;
}
default:
{
context.HttpContext.Response.StatusCode = 500;
context.HttpContext.Response.ContentType = "application/json";

var response = new OkResponse(false, "服务器内部错误");

logger.LogError(ex, "An error occurred while processing {Path}", context.HttpContext.Request.Path);

return response;
}
}
}
}
}
86 changes: 0 additions & 86 deletions src/NuGet.Next/Middlewares/ExceptionMiddleware.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/NuGet.Next/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using NuGet.Next;
using NuGet.Next.Converters;
using NuGet.Next.Extensions;
using NuGet.Next.Middlewares;
using NuGet.Next.Service;

Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
Expand Down Expand Up @@ -57,8 +56,6 @@

app.Configure(builder.Environment, builder.Configuration);

app.UseMiddleware<ExceptionMiddleware>();

app.UseResponseCompression();

app.UseStaticFiles();
Expand Down

0 comments on commit 68579e9

Please sign in to comment.