export class ResponseError extends Error {
constructor(errCode : number, errMsg : string,data:any|null) {
super(errMsg);
this.errCode = errCode;
this.errMsg = errMsg;
this.data = data
}
/**
* 错误代码。
*/
errCode : number;
/**
* 错误消息
*/
errMsg : string;
data ?: any | null;
}
protected async Request2(apiPath : string, method : "GET" | "POST" | "PUT" | "PATCH" | "DELETE", data : UTSJSONObject | null) : Promise<unknown> {
return new Promise((resolve, reject) => {
// 发送请求
uni.request({
url: this.ServerAddress + apiPath,
data,
method,
success: (res) => {
console.log(res);
if (res.statusCode == 200) {
resolve(res.data)
}
else {;
reject(new ResponseError(res.statusCode,"请求异常",res.data))
}
},
fail(err) {
reject(new ResponseError(err.errCode,err.errMsg,err.data))
}
})
});
}
/**
* 用户登录
*/
public Login(data : UTSJSONObject | null) {
this.Request2("/Token", "POST", {account:'test',password:'kkk123'}).then().catch((res) => {
console.log(res as ResponseError);
})
}
3***@qq.com (作者)
用console.log在控制台打印,打印出来的内容起始位置显示UTSError。使用类型断言把catch中捕获的res as ResponseError,在控制台打印还是只有打印出了name, message, cause三个属性。
2023-10-25 15:37
3***@qq.com (作者)
神奇!直接访问errCode,errMsg这些属性都可以访问到。谢谢!
2023-10-25 16:03