這只是我的代碼的例子
這只是我的代碼的例子
async function thisThrows() {
throw new Error("throw 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)。
});
下面的代碼片段給了我嵌套的錯誤輸出 i.e Error: 錯誤。錯誤
Error: Error: Error: Thrown from thisThrows()
at run1 (/Users/saunish/servify/sandbox/error-handling.js:18:15)
我需要輸出為
Error。Thrown from thisThrows()
uj5u.com熱心網友回復:
這是因為你在捕捉錯誤,然后創建新的錯誤,并拋出新的錯誤而不是原來的錯誤。
所有的函式實際上都應該重新拋出原始錯誤,例如:
async function run() {
try {
await thisThrows();
} catch (e) {
throw e // just rethrow the original。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/334224.html
標籤:
