首页 > 编程语言 >[ES2024] Improve Application-wide Error Handling rethrowing JavaScript Error with the Error Cause

[ES2024] Improve Application-wide Error Handling rethrowing JavaScript Error with the Error Cause

时间:2024-05-22 14:41:56浏览次数:25  
标签:wide Handling console log err JavaScript Error cause

The new cause data property that you can add to a thrown Error can be used to retain access to the original error caught in a promise rejection.

 

const sendLog = (...args) => console.log(...args);

async function fetchStuff() {
    await fetch('http://doesnt.exist/stuff')
        .catch(err => {
            sendLog(err);
            throw new Error('Loading stuff failed', { cause: err });
        });
}

fetchStuff()
    .catch(err => {
        console.log(err);
        console.log(err.cause);
    });

标签:wide,Handling,console,log,err,JavaScript,Error,cause
From: https://www.cnblogs.com/Answer1215/p/18206199

相关文章