feat(common): 新增基础设施 HAR(网络/鉴权/日志/状态/工具)
This commit is contained in:
26
common/src/main/ets/network/ApiError.ets
Normal file
26
common/src/main/ets/network/ApiError.ets
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* 统一错误模型:业务错误码 / HTTP 状态码 / 网络异常。
|
||||
* 继承 Error 以符合 ArkTS 的 throw 限制(只能抛 Error 及其子类)。
|
||||
*/
|
||||
export class ApiError extends Error {
|
||||
readonly code: number;
|
||||
readonly isNetworkError: boolean;
|
||||
|
||||
constructor(code: number, message: string, isNetworkError: boolean = false) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
this.isNetworkError = isNetworkError;
|
||||
}
|
||||
|
||||
static network(detail: string): ApiError {
|
||||
return new ApiError(-1, `网络请求失败: ${detail}`, true);
|
||||
}
|
||||
|
||||
static http(statusCode: number): ApiError {
|
||||
return new ApiError(statusCode, `HTTP 状态码异常: ${statusCode}`, false);
|
||||
}
|
||||
|
||||
static business(code: number, message: string): ApiError {
|
||||
return new ApiError(code, message, false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user