Chain errors with the cause option

Also on X

When throwing a new error, pass the original one as cause. Debugging gets easier because the original error and its stack trace are kept.

try {
  connectToDatabase();
} catch (err) {
  throw new Error("Connecting to database failed.", { cause: err });
}

This way, the logs show both what failed and why. MDN: Error.cause

← All TILs