Skip to content
Advertisement

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

Is there a standard / best practice way to add a cause of an exception in javascript. In java you might do this:

Throwable t = new Exception("whatever");
t.addCause(previouslyCaughtException);
throw t;

When the resulting exception is printed, it’ll give you a nice trace that includes causes. Is there any good way to do this in javascript or do I have to roll my own?

Advertisement

Answer

In prod, we use TraceError

Usage

import TraceError from 'trace-error';

global.TraceError = TraceError; // expose globally (optional)

throw new TraceError('Could not set status', srcError, ...otherErrors);

Output

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement