-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
92 deletions.
There are no files selected for viewing
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
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
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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