How to chain exceptions in javascript (ie add cause like in java)

For now (until there’s a better answer), this is what I’ve done:

...
} catch(e) {
  throw new Error("My error message, caused by: "+e.stack+"\n ------The above causes:-----")
}

The way I’m printing exceptions makes it nice and clean looking:

console.log(e.stack)

prints something like this:

My error message: SomeError
<some line>
<more lines>
------The above causes:-----
<some line>
<more lines>

The line might be better if it said “causes” because the stack trace of the exception causing the error is printed first.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)