Skip to content

Commit ebceb0a

Browse files
committed
Error系列全局可用
1 parent 162b92a commit ebceb0a

10 files changed

Lines changed: 84 additions & 86 deletions

Controllers/SimApiAuthController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Collections.Generic;
2-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Mvc;
32
using SimApi.Attributes;
43
using SimApi.Communications;
54
using SimApi.Helpers;
Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Diagnostics.CodeAnalysis;
2-
using SimApi.Communications;
1+
using SimApi.Communications;
32
using Microsoft.AspNetCore.Mvc;
43
using Microsoft.AspNetCore.Mvc.Filters;
54
using Microsoft.AspNetCore.Mvc.ModelBinding;
@@ -38,74 +37,4 @@ public override void OnActionExecuting(ActionExecutingContext context)
3837
break;
3938
}
4039
}
41-
42-
43-
/// <summary>
44-
/// 错误返回
45-
/// </summary>
46-
/// <param name="code">错误代码</param>
47-
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
48-
/// <returns></returns>
49-
protected static void Error(int code = 500, string message = "")
50-
{
51-
throw new SimApiException(code, message);
52-
}
53-
54-
/// <summary>
55-
/// 检测条件,根据条件返回报错 如果condition是ture报错
56-
/// </summary>
57-
/// <param name="condition">检测条件</param>
58-
/// <param name="code">错误代码</param>
59-
/// <param name="message">错误描述</param>
60-
protected static void ErrorWhen([DoesNotReturnIf(true)] bool condition, int code = 400, string message = "")
61-
{
62-
if (condition)
63-
{
64-
Error(code, message);
65-
}
66-
}
67-
68-
69-
/// <summary>
70-
/// 检测条件,根据条件返回报错 如果condition是ture报错
71-
/// </summary>
72-
/// <param name="condition">检测条件</param>
73-
/// <param name="code">错误代码</param>
74-
/// <param name="message">错误描述</param>
75-
protected static void ErrorWhenTrue([DoesNotReturnIf(true)] bool condition, int code = 400, string message = "")
76-
{
77-
ErrorWhen(condition, code, message);
78-
}
79-
80-
81-
/// <summary>
82-
/// 如果condition是false 报错
83-
/// </summary>
84-
/// <param name="condition"></param>
85-
/// <param name="code"></param>
86-
/// <param name="message"></param>
87-
protected static void ErrorWhenFalse([DoesNotReturnIf(false)] bool condition, int code = 400, string message = "")
88-
{
89-
ErrorWhen(!condition, code, message);
90-
}
91-
92-
/// <summary>
93-
/// 检测给定的变量是否为NUll
94-
/// </summary>
95-
/// <param name="condition">检测条件</param>
96-
/// <param name="code">错误代码</param>
97-
/// <param name="message">错误描述</param>
98-
protected static void ErrorWhenNull([NotNull] object? condition, int code = 404, string message = "请求的资源不存在")
99-
{
100-
ErrorWhen(condition == null, code, message);
101-
}
102-
103-
/// <summary>
104-
/// 上传文件
105-
/// </summary>
106-
/// <returns></returns>
107-
protected SimApiBaseResponse<string> UploadFile()
108-
{
109-
return new SimApiBaseResponse<string>();
110-
}
11140
}

GlobalUsing.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using static SimApiErrorExtension;

SimApiErrorExtension.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using SimApi.Exceptions;
3+
4+
5+
public static class SimApiErrorExtension
6+
{
7+
/// <summary>
8+
/// 错误返回
9+
/// </summary>
10+
/// <param name="code">错误代码</param>
11+
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
12+
/// <returns></returns>
13+
public static void Error(int code = 500, string message = "")
14+
{
15+
throw new SimApiException(code, message);
16+
}
17+
18+
/// <summary>
19+
/// 检测条件,根据条件返回报错 如果condition是ture报错
20+
/// </summary>
21+
/// <param name="condition">检测条件</param>
22+
/// <param name="code">错误代码</param>
23+
/// <param name="message">错误描述</param>
24+
public static void ErrorWhen([DoesNotReturnIf(true)] bool condition, int code = 400,
25+
string message = "")
26+
{
27+
if (condition)
28+
{
29+
Error(code, message);
30+
}
31+
}
32+
33+
34+
/// <summary>
35+
/// 检测条件,根据条件返回报错 如果condition是ture报错
36+
/// </summary>
37+
/// <param name="condition">检测条件</param>
38+
/// <param name="code">错误代码</param>
39+
/// <param name="message">错误描述</param>
40+
public static void ErrorWhenTrue([DoesNotReturnIf(true)] bool condition, int code = 400,
41+
string message = "")
42+
{
43+
ErrorWhen(condition, code, message);
44+
}
45+
46+
47+
/// <summary>
48+
/// 如果condition是false 报错
49+
/// </summary>
50+
/// <param name="condition"></param>
51+
/// <param name="code"></param>
52+
/// <param name="message"></param>
53+
public static void ErrorWhenFalse([DoesNotReturnIf(false)] bool condition, int code = 400,
54+
string message = "")
55+
{
56+
ErrorWhen(!condition, code, message);
57+
}
58+
59+
/// <summary>
60+
/// 检测给定的变量是否为NUll
61+
/// </summary>
62+
/// <param name="condition">检测条件</param>
63+
/// <param name="code">错误代码</param>
64+
/// <param name="message">错误描述</param>
65+
public static void ErrorWhenNull([NotNull] object? condition, int code = 404,
66+
string message = "请求的资源不存在")
67+
{
68+
ErrorWhen(condition == null, code, message);
69+
}
70+
}

SimApiExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics;
43
using System.Linq;
54
using System.Reflection;
@@ -136,14 +135,14 @@ string GetSimpleTypeName(Type t, int depth = 0)
136135
if (t.IsArray)
137136
{
138137
var elementType = t.GetElementType();
139-
return $"{GetSimpleTypeName(elementType, depth + 1)}[]";
138+
return $"{GetSimpleTypeName(elementType!, depth + 1)}[]";
140139
}
141140

142141
// 处理可空类型
143142
if (Nullable.GetUnderlyingType(t) != null)
144143
{
145144
var underlyingType = Nullable.GetUnderlyingType(t);
146-
return GetSimpleTypeName(underlyingType, depth + 1);
145+
return GetSimpleTypeName(underlyingType!, depth + 1);
147146
}
148147

149148
// 处理泛型类型(递归解析嵌套泛型)

SwaggerFilters/AesBodyOperationFilter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
2222
.GetCustomAttribute<AesBodyAttribute>() != null;
2323
if (!hasAesBodyAttr) continue;
2424
// 1. 移除默认的 Query 参数描述(如果存在)
25-
var queryParam = operation.Parameters
25+
var queryParam = operation.Parameters!
2626
.FirstOrDefault(p => p.Name == parameter.Name);
2727
if (queryParam != null)
2828
{
29-
operation.Parameters.Remove(queryParam);
29+
operation.Parameters!.Remove(queryParam);
3030
}
3131

3232
// 2. 添加 Body 参数描述

SwaggerFilters/GlobalDynamicObjectSchemaFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
1313
{
1414
if (!IsDynamicObjectType(context.Type)) return;
1515
var oaSchema = schema as OpenApiSchema;
16-
oaSchema.AdditionalPropertiesAllowed = true;
16+
oaSchema!.AdditionalPropertiesAllowed = true;
1717
oaSchema.AdditionalProperties = new OpenApiSchema
1818
{
1919
Type = JsonSchemaType.Object, // 表示 value 可以是任意类型(兼容所有类型)

SwaggerFilters/RemoveEmptyTagsFilter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
1010
{
1111
// 步骤1:收集所有有接口的 Tag 名称
1212
var tagsWithOperations = swaggerDoc.Paths.Values
13-
.SelectMany(path => path.Operations.Values)
14-
.SelectMany(op => op.Tags.Select(t => t.Name))
13+
.SelectMany(path => path.Operations!.Values)
14+
.SelectMany(op => op.Tags!.Select(t => t.Name))
1515
.Distinct()
1616
.ToList();
1717

1818
// 步骤2:移除无接口的空 Tag
19-
var emptyTags = swaggerDoc.Tags.Where(t => !tagsWithOperations.Contains(t.Name)).ToList();
19+
var emptyTags = swaggerDoc.Tags!.Where(t => !tagsWithOperations.Contains(t.Name)).ToList();
2020
foreach (var tag in emptyTags)
2121
{
22-
swaggerDoc.Tags.Remove(tag);
22+
swaggerDoc.Tags!.Remove(tag);
2323
}
2424
}
2525
}

SwaggerFilters/SimApiResponseOperationFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
4646
var schema = context.SchemaGenerator.GenerateSchema(wrappedType, context.SchemaRepository);
4747

4848
// 5. 替换 Swagger 文档中的响应类型(只保留 200 OK 的响应,匹配过滤器逻辑)
49-
operation.Responses.Clear(); // 清除默认响应(如 200 返回原始类型)
49+
operation.Responses!.Clear(); // 清除默认响应(如 200 返回原始类型)
5050
operation.Responses.Add("200", new OpenApiResponse
5151
{
5252
Description = "请求成功",

csharp-simapi-coding-style.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ string appId
323323

324324
### 7.7 错误处理
325325

326-
使用基类的 `ErrorWhen` 系列方法,**不手动 throw、不返回错误码**
326+
使用基类的 `ErrorWhen` 系列方法,**不手动 throw、不返回错误码**(全局扩展,任何地方都可以这样抛出异常)
327327

328328
```csharp
329329
ErrorWhenNull(entity); // null 则报错(默认 404)

0 commit comments

Comments
 (0)