如何处理节点js嵌套错误

how to handle node js nested errors

这只是我的代码示例

async function thisThrows() {
    throw new Error("Thrown from thisThrows()");
}

async function run() {
    try {
        await thisThrows();
    } catch (e) {
        throw new Error(e)
    }
}


async function run1() {
    try{
        await run()
    }catch(e){
        throw new Error(e);
    }
}

run1().catch(error => {
    console.log(error);
});

下面的代码片段给我嵌套的错误输出 即错误:错误:错误

Error: Error: Error: Thrown from thisThrows()
    at run1 (/Users/saunish/servify/sandbox/error-handling.js:18:15)

我需要输出为

Error: Thrown from thisThrows()

这是因为您正在捕获错误,然后创建新错误并抛出新错误而不是原始错误。

所有函数实际上应该重新抛出原始错误,例如:

async function run() {
    try {
        await thisThrows();
    } catch (e) {
        throw e // just rethrow the original
    }
}