Skip to content

Commit

Permalink
✨ feat: 优化 PluginErrorType 实现
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 25, 2023
1 parent 9fc023d commit fbc5425
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
export enum ErrorType {
/* eslint-disable sort-keys-fix/sort-keys-fix */
export const PluginErrorType = {
// ******* 业务错误语义 ******* //
PluginMarketIndexNotFound = 'pluginMarketIndexNotFound', // 插件市场索引解析失败
PluginMarketIndexInvalid = 'pluginMarketIndexInvalid', // 插件市场索引无效
PluginMarketIndexNotFound: 'pluginMarketIndexNotFound', // 插件市场索引解析失败
PluginMarketIndexInvalid: 'pluginMarketIndexInvalid', // 插件市场索引无效

PluginMetaNotFound = 'pluginMetaNotFound', // 没有找到插件元数据
PluginMetaInvalid = 'pluginMetaInvalid', // 插件元数据无效
PluginMetaNotFound: 'pluginMetaNotFound', // 没有找到插件元数据
PluginMetaInvalid: 'pluginMetaInvalid', // 插件元数据无效

PluginManifestNotFound = 'pluginManifestNotFound', // 插件描述文件不存在
PluginManifestInvalid = 'pluginManifestInvalid', // 插件描述文件不存在
PluginManifestNotFound: 'pluginManifestNotFound', // 插件描述文件不存在
PluginManifestInvalid: 'pluginManifestInvalid', // 插件描述文件不存在

// ******* 客户端错误 ******* //
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
ContentNotFound = 404, // 没找到接口
MethodNotAllowed = 405, // 不支持
TooManyRequests = 429,
BadRequest: 400,
Unauthorized: 401,
Forbidden: 403,
ContentNotFound: 404, // 没找到接口
MethodNotAllowed: 405, // 不支持
TooManyRequests: 429,

// ******* 服务端错误 ******* //
InternalServerError = 500,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
}
InternalServerError: 500,
BadGateway: 502,
ServiceUnavailable: 503,
GatewayTimeout: 504,
};
/* eslint-enable */

export type IPluginErrorType = keyof typeof PluginErrorType;

const getStatus = (errorType: ErrorType | string) => {
const getStatus = (errorType: IPluginErrorType | string) => {
switch (errorType) {
case ErrorType.PluginMetaNotFound:
case ErrorType.PluginManifestNotFound:
case PluginErrorType.PluginMetaNotFound:
case PluginErrorType.PluginManifestNotFound:
return 404;

case ErrorType.PluginMetaInvalid:
case PluginErrorType.PluginMetaInvalid:
return 490;
case ErrorType.PluginManifestInvalid:
case PluginErrorType.PluginManifestInvalid:
return 491;

case ErrorType.PluginMarketIndexNotFound:
case PluginErrorType.PluginMarketIndexNotFound:
return 590;

case ErrorType.PluginMarketIndexInvalid:
case PluginErrorType.PluginMarketIndexInvalid:
return 590;
}

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

export interface ErrorResponse {
body: any;
errorType: ErrorType | string;
errorType: IPluginErrorType | string;
}

/**
* 创建一个错误响应对象
* @param {ErrorType} errorType - 错误类型
* @param {IPluginErrorType} errorType - 错误类型
* @param body - 响应体数据
* @returns {Response} - 错误响应对象
*/
export const createErrorResponse = (errorType: ErrorType | string, body?: string | object) => {
export const createErrorResponse = (
errorType: IPluginErrorType | string,
body?: string | object,
): Response => {
// 获取错误类型对应的状态码
const statusCode = getStatus(errorType);

Expand Down

0 comments on commit fbc5425

Please sign in to comment.