Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aef62a3472 |
@@ -0,0 +1,6 @@
|
|||||||
|
namespace SimApi.Configurations;
|
||||||
|
|
||||||
|
public class SimApiExceptionOptions
|
||||||
|
{
|
||||||
|
public int[] SkipStatusCodes = [200, 301, 302];
|
||||||
|
}
|
||||||
@@ -113,6 +113,13 @@ public class SimApiOptions
|
|||||||
|
|
||||||
public SimApiHttpClientOptions SimApiHttpClientOptions { get; set; } = new();
|
public SimApiHttpClientOptions SimApiHttpClientOptions { get; set; } = new();
|
||||||
|
|
||||||
|
public SimApiExceptionOptions SimApiExceptionOptions { get; set; } = new();
|
||||||
|
|
||||||
|
public void ConfigureSimApiException(Action<SimApiExceptionOptions>? options = null)
|
||||||
|
{
|
||||||
|
options?.Invoke(SimApiExceptionOptions);
|
||||||
|
}
|
||||||
|
|
||||||
public void ConfigureSimApiHttpClient(Action<SimApiHttpClientOptions>? options = null)
|
public void ConfigureSimApiHttpClient(Action<SimApiHttpClientOptions>? options = null)
|
||||||
{
|
{
|
||||||
options?.Invoke(SimApiHttpClientOptions);
|
options?.Invoke(SimApiHttpClientOptions);
|
||||||
|
|||||||
@@ -1,16 +1,22 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SimApi.Configurations;
|
||||||
using SimApi.Exceptions;
|
using SimApi.Exceptions;
|
||||||
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Middlewares;
|
namespace SimApi.Middlewares;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异常处理中间件
|
/// 异常处理中间件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
|
public class SimApiExceptionMiddleware(
|
||||||
|
RequestDelegate next,
|
||||||
|
ILogger<SimApiExceptionMiddleware> log,
|
||||||
|
SimApiOptions simApiOptions)
|
||||||
{
|
{
|
||||||
public async Task InvokeAsync(HttpContext context)
|
public async Task InvokeAsync(HttpContext context)
|
||||||
{
|
{
|
||||||
@@ -24,18 +30,9 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
|
|||||||
await next(context);
|
await next(context);
|
||||||
if (!context.Response.HasStarted)
|
if (!context.Response.HasStarted)
|
||||||
{
|
{
|
||||||
switch (context.Response.StatusCode)
|
SimApiError.ErrorWhenFalse(
|
||||||
{
|
simApiOptions.SimApiExceptionOptions.SkipStatusCodes.Contains(context.Response.StatusCode),
|
||||||
case 200:
|
context.Response.StatusCode);
|
||||||
case 301:
|
|
||||||
case 302:
|
|
||||||
case 404:
|
|
||||||
break;
|
|
||||||
// case 404:
|
|
||||||
// throw new SimApiException(context.Response.StatusCode, "请求的接口不存在");
|
|
||||||
default:
|
|
||||||
throw new SimApiException(context.Response.StatusCode);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
Reference in New Issue
Block a user