Skip to content

Commit fbc5425

Browse files
committed
✨ feat: 优化 PluginErrorType 实现
1 parent 9fc023d commit fbc5425

1 file changed

Lines changed: 35 additions & 28 deletions

File tree

src/error.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
1-
export enum ErrorType {
1+
/* eslint-disable sort-keys-fix/sort-keys-fix */
2+
export const PluginErrorType = {
23
// ******* 业务错误语义 ******* //
3-
PluginMarketIndexNotFound = 'pluginMarketIndexNotFound', // 插件市场索引解析失败
4-
PluginMarketIndexInvalid = 'pluginMarketIndexInvalid', // 插件市场索引无效
4+
PluginMarketIndexNotFound: 'pluginMarketIndexNotFound', // 插件市场索引解析失败
5+
PluginMarketIndexInvalid: 'pluginMarketIndexInvalid', // 插件市场索引无效
56

6-
PluginMetaNotFound = 'pluginMetaNotFound', // 没有找到插件元数据
7-
PluginMetaInvalid = 'pluginMetaInvalid', // 插件元数据无效
7+
PluginMetaNotFound: 'pluginMetaNotFound', // 没有找到插件元数据
8+
PluginMetaInvalid: 'pluginMetaInvalid', // 插件元数据无效
89

9-
PluginManifestNotFound = 'pluginManifestNotFound', // 插件描述文件不存在
10-
PluginManifestInvalid = 'pluginManifestInvalid', // 插件描述文件不存在
10+
PluginManifestNotFound: 'pluginManifestNotFound', // 插件描述文件不存在
11+
PluginManifestInvalid: 'pluginManifestInvalid', // 插件描述文件不存在
1112

1213
// ******* 客户端错误 ******* //
13-
BadRequest = 400,
14-
Unauthorized = 401,
15-
Forbidden = 403,
16-
ContentNotFound = 404, // 没找到接口
17-
MethodNotAllowed = 405, // 不支持
18-
TooManyRequests = 429,
14+
BadRequest: 400,
15+
Unauthorized: 401,
16+
Forbidden: 403,
17+
ContentNotFound: 404, // 没找到接口
18+
MethodNotAllowed: 405, // 不支持
19+
TooManyRequests: 429,
1920

2021
// ******* 服务端错误 ******* //
21-
InternalServerError = 500,
22-
BadGateway = 502,
23-
ServiceUnavailable = 503,
24-
GatewayTimeout = 504,
25-
}
22+
InternalServerError: 500,
23+
BadGateway: 502,
24+
ServiceUnavailable: 503,
25+
GatewayTimeout: 504,
26+
};
27+
/* eslint-enable */
28+
29+
export type IPluginErrorType = keyof typeof PluginErrorType;
2630

27-
const getStatus = (errorType: ErrorType | string) => {
31+
const getStatus = (errorType: IPluginErrorType | string) => {
2832
switch (errorType) {
29-
case ErrorType.PluginMetaNotFound:
30-
case ErrorType.PluginManifestNotFound:
33+
case PluginErrorType.PluginMetaNotFound:
34+
case PluginErrorType.PluginManifestNotFound:
3135
return 404;
3236

33-
case ErrorType.PluginMetaInvalid:
37+
case PluginErrorType.PluginMetaInvalid:
3438
return 490;
35-
case ErrorType.PluginManifestInvalid:
39+
case PluginErrorType.PluginManifestInvalid:
3640
return 491;
3741

38-
case ErrorType.PluginMarketIndexNotFound:
42+
case PluginErrorType.PluginMarketIndexNotFound:
3943
return 590;
4044

41-
case ErrorType.PluginMarketIndexInvalid:
45+
case PluginErrorType.PluginMarketIndexInvalid:
4246
return 590;
4347
}
4448

@@ -49,16 +53,19 @@ const getStatus = (errorType: ErrorType | string) => {
4953

5054
export interface ErrorResponse {
5155
body: any;
52-
errorType: ErrorType | string;
56+
errorType: IPluginErrorType | string;
5357
}
5458

5559
/**
5660
* 创建一个错误响应对象
57-
* @param {ErrorType} errorType - 错误类型
61+
* @param {IPluginErrorType} errorType - 错误类型
5862
* @param body - 响应体数据
5963
* @returns {Response} - 错误响应对象
6064
*/
61-
export const createErrorResponse = (errorType: ErrorType | string, body?: string | object) => {
65+
export const createErrorResponse = (
66+
errorType: IPluginErrorType | string,
67+
body?: string | object,
68+
): Response => {
6269
// 获取错误类型对应的状态码
6370
const statusCode = getStatus(errorType);
6471

0 commit comments

Comments
 (0)