function doFoo() {
throw new Error("no foo for you");
}
try {
doFoo();
} catch(e) {
throw new Error("doFoo() for shame!", e);
}
Is there any sane way to do this in JavaScript?
My specific objectives for the particular use case I'm asking this question are the following, in this order:
- Follow standards, or at least best practices
- Throw a new exception/error/whatever you want to call it (not just rethrow the thing I just caught); I'll call this an exception from now on
- Preserve the message in the previous exception (I'd like to preserve the stack trace, but that's not as important for this particular use case)
- Add extra metadata to the previous exception
- Preserve the stack trace of the previous exception (nice to have, not really important to me right now)