-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ErrorResponse, ErrorType } from '@/types/error'; | ||
|
||
const getStatus = (errorType: ErrorType) => { | ||
// TODO: Add ErrorSwitch | ||
// eslint-disable-next-line no-empty | ||
switch (errorType) { | ||
} | ||
|
||
return errorType; | ||
}; | ||
|
||
/** | ||
* 创建一个错误响应对象 | ||
* @param {ErrorType} errorType - 错误类型 | ||
* @param body - 响应体数据 | ||
* @returns {Response} - 错误响应对象 | ||
*/ | ||
export const createErrorResponse = (errorType: ErrorType, body?: string | object) => { | ||
// 获取错误类型对应的状态码 | ||
const statusCode = getStatus(errorType); | ||
|
||
// 构造错误响应数据 | ||
const data: ErrorResponse = { body, errorType }; | ||
|
||
// 创建并返回错误响应对象 | ||
return new Response(JSON.stringify(data), { status: statusCode }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export enum ErrorType { | ||
// ******* 业务错误语义 ******* // | ||
|
||
// ******* 客户端错误 ******* // | ||
BadRequest = 400, | ||
Unauthorized = 401, | ||
Forbidden = 403, | ||
ContentNotFound = 404, // 没找到接口 | ||
MethodNotAllowed = 405, // 不支持 | ||
TooManyRequests = 429, | ||
|
||
// ******* 服务端错误 ******* // | ||
InternalServerError = 500, | ||
BadGateway = 502, | ||
ServiceUnavailable = 503, | ||
GatewayTimeout = 504, | ||
} | ||
|
||
export interface ErrorResponse { | ||
body: any; | ||
errorType: ErrorType; | ||
} |