Typescript 如何编写模拟Gaxios错误的类?
我正在使用GoogleSDK与带有TypeScript的管理api进行通信。Google sdk使用Gaxios发送请求。我想从GoogleAPI为方法编写错误处理。我想检查错误是否属于Gaxios错误类型,如果是,我将抛出相应的http错误Typescript 如何编写模拟Gaxios错误的类?,typescript,google-api,instanceof,gaxios,Typescript,Google Api,Instanceof,Gaxios,我正在使用GoogleSDK与带有TypeScript的管理api进行通信。Google sdk使用Gaxios发送请求。我想从GoogleAPI为方法编写错误处理。我想检查错误是否属于Gaxios错误类型,如果是,我将抛出相应的http错误 export const handleError = (error) => { ... // Handling errors from google sdk if(error instanceof GaxiosError){
export const handleError = (error) => {
...
// Handling errors from google sdk
if(error instanceof GaxiosError){
return Handle(400, error.message)
}
return handle(500, "Unknown error");
};
问题是我无法编写一个将通过instanceof的类,我不想安装整个Gaxios库来只处理google sdk错误
这是Gaxios的代码
这是我的代码:
export interface GaxiosResponse<T = any> {
config: any;
data: T;
status: number;
statusText: string;
headers: any;
request: any;
}
export interface GaxiosOptions {}
export class GaxiosError<T = any> extends Error {
code?: string;
response?: GaxiosResponse<T>;
config: GaxiosOptions;
constructor(message: string, options: GaxiosOptions, response: GaxiosResponse<T>) {
super(message);
this.response = response;
this.config = options;
this.code = response.status.toString();
}
}
导出接口间隙响应{
配置:任意;
资料:T;
状态:编号;
statusText:字符串;
标题:任何;
请求:任何;
}
导出接口GaxiosOptions{}
导出类GaxiosError扩展错误{
代码?:字符串;
反应?:同性恋反应;
配置:GaxiosOptions;
构造函数(消息:字符串,选项:GaxiosOptions,响应:GaxiosResponse){
超级(信息);
这个。反应=反应;
this.config=选项;
this.code=response.status.toString();
}
}