Vla*_*gas 6 typescript ecmascript-6
我想抛出一个带有内部错误的错误,但错误构造函数没有内部错误参数。
例如,在 c# 中我执行以下操作:
try
{
var a = 3;
var b = 0;
var c = a/b;
}
catch(Exception ex)
{
throw new Exception("Your custom message", ex);
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能用打字稿做类似的事情?也许是 NPM 包?我一直在寻找它,但没有找到任何相关的包
您需要定义自定义错误类。它会类似于:
export class CustomError extends Error {
public innerError: Error | undefined;
// any other variable definition if needed
public constructor(message?: string, innerError?: Error) {
super(message);
this.innerError = innerError;
// any other logic or variable assignment if needed
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的代码中:
try
{
const a = 3;
const b = 0;
const c = a/b;
} catch(ex) {
throw new CustomError("Your custom message", ex as Error);
}
Run Code Online (Sandbox Code Playgroud)