The error message might sometimes be used for a public-facing message. You could then put more technical information in the cause as well. That's my guess, at least.
At least, I've never heard of anyone using exceptions for display purposes - exceptions generally do contain technical information, they are usually intended for developers, not for users. How would you deal with localization, for instance... That's just not what exceptions are for...
Thanks for attaching this link for reference. ๐
The way this would be super helpful is in deeply nested functions with chained errors.
Like if we have mutiple try catch block inside a deeply nested function so we pass the string as the purpose of that block and cause as the argument of catch block. For example:
functiondeeplyNested(){try{// outer block logic.letappData=[{...}]// app data array parsing.appData.map((data)=>{try{// ยทยทยท}catch(error){thrownewError(`While processing ${data}`,{cause:error});}});}catch(error){throwError('Error while processing outer block',{cause:error});}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
What is
error.cause
for?Normally, the cause of the error is what I would put in the error message. I mean, what else would you put there?
Why do we need another field for that?
I don't know of any other language that has two text messages in the same exception. What for? ๐คทโโ๏ธ
The error message might sometimes be used for a public-facing message. You could then put more technical information in the cause as well. That's my guess, at least.
That sounds unlikely?
At least, I've never heard of anyone using exceptions for display purposes - exceptions generally do contain technical information, they are usually intended for developers, not for users. How would you deal with localization, for instance... That's just not what exceptions are for...
Ah, mystery solved!
2ality.com/2021/06/error-cause.html
It's for chaining errors - like the "inner exception" you have in many other languages.
Because you can throw anything in JS, the property type isn't specified as Error - but the intention is similar.
Thanks for attaching this link for reference. ๐
The way this would be super helpful is in deeply nested functions with chained errors.
Like if we have mutiple try catch block inside a deeply nested function so we pass the string as the purpose of that block and cause as the argument of catch block. For example: