//It contains 2 properties: message : the error description,
//a human readable message thatshould explain what error happened.
//name : the type of error occurred (assumes the value of the specific error object name,
//for example, TypeError or SyntaxError )
//Custom properties of error
function doWork() {
try {
doFailSomeWay();
} catch (err) {
throw new Error('Failed in some way', { cause: err });
}
try {
doFailAnotherWay();
} catch (err) {
throw new Error('Failed in another way', { cause: err });
}
}
try {
doWork();
} catch (err) {
switch(err.message) {
case 'Failed in some way':
handleFailSomeWay(err.cause);
break;
case 'Failed in another way':
handleFailAnotherWay(err.cause);
break;
}
}