Error系列全局可用
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using SimApi.Attributes;
|
using SimApi.Attributes;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
using SimApi.Communications;
|
||||||
using SimApi.Communications;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
@@ -38,74 +37,4 @@ public class SimApiBaseController : Controller
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 错误返回
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="code">错误代码</param>
|
|
||||||
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected static void Error(int code = 500, string message = "")
|
|
||||||
{
|
|
||||||
throw new SimApiException(code, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 检测条件,根据条件返回报错 如果condition是ture报错
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="condition">检测条件</param>
|
|
||||||
/// <param name="code">错误代码</param>
|
|
||||||
/// <param name="message">错误描述</param>
|
|
||||||
protected static void ErrorWhen([DoesNotReturnIf(true)] bool condition, int code = 400, string message = "")
|
|
||||||
{
|
|
||||||
if (condition)
|
|
||||||
{
|
|
||||||
Error(code, message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 检测条件,根据条件返回报错 如果condition是ture报错
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="condition">检测条件</param>
|
|
||||||
/// <param name="code">错误代码</param>
|
|
||||||
/// <param name="message">错误描述</param>
|
|
||||||
protected static void ErrorWhenTrue([DoesNotReturnIf(true)] bool condition, int code = 400, string message = "")
|
|
||||||
{
|
|
||||||
ErrorWhen(condition, code, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 如果condition是false 报错
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="condition"></param>
|
|
||||||
/// <param name="code"></param>
|
|
||||||
/// <param name="message"></param>
|
|
||||||
protected static void ErrorWhenFalse([DoesNotReturnIf(false)] bool condition, int code = 400, string message = "")
|
|
||||||
{
|
|
||||||
ErrorWhen(!condition, code, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 检测给定的变量是否为NUll
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="condition">检测条件</param>
|
|
||||||
/// <param name="code">错误代码</param>
|
|
||||||
/// <param name="message">错误描述</param>
|
|
||||||
protected static void ErrorWhenNull([NotNull] object? condition, int code = 404, string message = "请求的资源不存在")
|
|
||||||
{
|
|
||||||
ErrorWhen(condition == null, code, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 上传文件
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected SimApiBaseResponse<string> UploadFile()
|
|
||||||
{
|
|
||||||
return new SimApiBaseResponse<string>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
global using static SimApiErrorExtension;
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using SimApi.Exceptions;
|
||||||
|
|
||||||
|
|
||||||
|
public static class SimApiErrorExtension
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 错误返回
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">错误代码</param>
|
||||||
|
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static void Error(int code = 500, string message = "")
|
||||||
|
{
|
||||||
|
throw new SimApiException(code, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检测条件,根据条件返回报错 如果condition是ture报错
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="condition">检测条件</param>
|
||||||
|
/// <param name="code">错误代码</param>
|
||||||
|
/// <param name="message">错误描述</param>
|
||||||
|
public static void ErrorWhen([DoesNotReturnIf(true)] bool condition, int code = 400,
|
||||||
|
string message = "")
|
||||||
|
{
|
||||||
|
if (condition)
|
||||||
|
{
|
||||||
|
Error(code, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检测条件,根据条件返回报错 如果condition是ture报错
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="condition">检测条件</param>
|
||||||
|
/// <param name="code">错误代码</param>
|
||||||
|
/// <param name="message">错误描述</param>
|
||||||
|
public static void ErrorWhenTrue([DoesNotReturnIf(true)] bool condition, int code = 400,
|
||||||
|
string message = "")
|
||||||
|
{
|
||||||
|
ErrorWhen(condition, code, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 如果condition是false 报错
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="condition"></param>
|
||||||
|
/// <param name="code"></param>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
public static void ErrorWhenFalse([DoesNotReturnIf(false)] bool condition, int code = 400,
|
||||||
|
string message = "")
|
||||||
|
{
|
||||||
|
ErrorWhen(!condition, code, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检测给定的变量是否为NUll
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="condition">检测条件</param>
|
||||||
|
/// <param name="code">错误代码</param>
|
||||||
|
/// <param name="message">错误描述</param>
|
||||||
|
public static void ErrorWhenNull([NotNull] object? condition, int code = 404,
|
||||||
|
string message = "请求的资源不存在")
|
||||||
|
{
|
||||||
|
ErrorWhen(condition == null, code, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-3
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@@ -136,14 +135,14 @@ public static class SimApiExtensions
|
|||||||
if (t.IsArray)
|
if (t.IsArray)
|
||||||
{
|
{
|
||||||
var elementType = t.GetElementType();
|
var elementType = t.GetElementType();
|
||||||
return $"{GetSimpleTypeName(elementType, depth + 1)}[]";
|
return $"{GetSimpleTypeName(elementType!, depth + 1)}[]";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理可空类型
|
// 处理可空类型
|
||||||
if (Nullable.GetUnderlyingType(t) != null)
|
if (Nullable.GetUnderlyingType(t) != null)
|
||||||
{
|
{
|
||||||
var underlyingType = Nullable.GetUnderlyingType(t);
|
var underlyingType = Nullable.GetUnderlyingType(t);
|
||||||
return GetSimpleTypeName(underlyingType, depth + 1);
|
return GetSimpleTypeName(underlyingType!, depth + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理泛型类型(递归解析嵌套泛型)
|
// 处理泛型类型(递归解析嵌套泛型)
|
||||||
|
|||||||
@@ -22,11 +22,11 @@ public class AesBodyOperationFilter : IOperationFilter
|
|||||||
.GetCustomAttribute<AesBodyAttribute>() != null;
|
.GetCustomAttribute<AesBodyAttribute>() != null;
|
||||||
if (!hasAesBodyAttr) continue;
|
if (!hasAesBodyAttr) continue;
|
||||||
// 1. 移除默认的 Query 参数描述(如果存在)
|
// 1. 移除默认的 Query 参数描述(如果存在)
|
||||||
var queryParam = operation.Parameters
|
var queryParam = operation.Parameters!
|
||||||
.FirstOrDefault(p => p.Name == parameter.Name);
|
.FirstOrDefault(p => p.Name == parameter.Name);
|
||||||
if (queryParam != null)
|
if (queryParam != null)
|
||||||
{
|
{
|
||||||
operation.Parameters.Remove(queryParam);
|
operation.Parameters!.Remove(queryParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 添加 Body 参数描述
|
// 2. 添加 Body 参数描述
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public class GlobalDynamicObjectSchemaFilter : ISchemaFilter
|
|||||||
{
|
{
|
||||||
if (!IsDynamicObjectType(context.Type)) return;
|
if (!IsDynamicObjectType(context.Type)) return;
|
||||||
var oaSchema = schema as OpenApiSchema;
|
var oaSchema = schema as OpenApiSchema;
|
||||||
oaSchema.AdditionalPropertiesAllowed = true;
|
oaSchema!.AdditionalPropertiesAllowed = true;
|
||||||
oaSchema.AdditionalProperties = new OpenApiSchema
|
oaSchema.AdditionalProperties = new OpenApiSchema
|
||||||
{
|
{
|
||||||
Type = JsonSchemaType.Object, // 表示 value 可以是任意类型(兼容所有类型)
|
Type = JsonSchemaType.Object, // 表示 value 可以是任意类型(兼容所有类型)
|
||||||
|
|||||||
@@ -10,16 +10,16 @@ public class RemoveEmptyTagsFilter : IDocumentFilter
|
|||||||
{
|
{
|
||||||
// 步骤1:收集所有有接口的 Tag 名称
|
// 步骤1:收集所有有接口的 Tag 名称
|
||||||
var tagsWithOperations = swaggerDoc.Paths.Values
|
var tagsWithOperations = swaggerDoc.Paths.Values
|
||||||
.SelectMany(path => path.Operations.Values)
|
.SelectMany(path => path.Operations!.Values)
|
||||||
.SelectMany(op => op.Tags.Select(t => t.Name))
|
.SelectMany(op => op.Tags!.Select(t => t.Name))
|
||||||
.Distinct()
|
.Distinct()
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
// 步骤2:移除无接口的空 Tag
|
// 步骤2:移除无接口的空 Tag
|
||||||
var emptyTags = swaggerDoc.Tags.Where(t => !tagsWithOperations.Contains(t.Name)).ToList();
|
var emptyTags = swaggerDoc.Tags!.Where(t => !tagsWithOperations.Contains(t.Name)).ToList();
|
||||||
foreach (var tag in emptyTags)
|
foreach (var tag in emptyTags)
|
||||||
{
|
{
|
||||||
swaggerDoc.Tags.Remove(tag);
|
swaggerDoc.Tags!.Remove(tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@ public class SimApiResponseOperationFilter : IOperationFilter
|
|||||||
var schema = context.SchemaGenerator.GenerateSchema(wrappedType, context.SchemaRepository);
|
var schema = context.SchemaGenerator.GenerateSchema(wrappedType, context.SchemaRepository);
|
||||||
|
|
||||||
// 5. 替换 Swagger 文档中的响应类型(只保留 200 OK 的响应,匹配过滤器逻辑)
|
// 5. 替换 Swagger 文档中的响应类型(只保留 200 OK 的响应,匹配过滤器逻辑)
|
||||||
operation.Responses.Clear(); // 清除默认响应(如 200 返回原始类型)
|
operation.Responses!.Clear(); // 清除默认响应(如 200 返回原始类型)
|
||||||
operation.Responses.Add("200", new OpenApiResponse
|
operation.Responses.Add("200", new OpenApiResponse
|
||||||
{
|
{
|
||||||
Description = "请求成功",
|
Description = "请求成功",
|
||||||
|
|||||||
@@ -323,7 +323,7 @@ string appId
|
|||||||
|
|
||||||
### 7.7 错误处理
|
### 7.7 错误处理
|
||||||
|
|
||||||
使用基类的 `ErrorWhen` 系列方法,**不手动 throw、不返回错误码**:
|
使用基类的 `ErrorWhen` 系列方法,**不手动 throw、不返回错误码**:(全局扩展,任何地方都可以这样抛出异常)
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
ErrorWhenNull(entity); // null 则报错(默认 404)
|
ErrorWhenNull(entity); // null 则报错(默认 404)
|
||||||
|
|||||||
Reference in New Issue
Block a user