Skip to content

Files

Latest commit

 

History

History
39 lines (30 loc) · 794 Bytes

no-throw-literal.md

File metadata and controls

39 lines (30 loc) · 794 Bytes

Pattern: Throwing non-Error objects

Issue: -

Description

Throwing literals or non-Error objects loses the stack trace and other debugging information that Error objects automatically capture. Always throw Error objects or instances of Error subclasses to maintain proper error tracking.

Examples

Example of incorrect code:

throw "error";
throw 500;
throw false;
throw null;
throw undefined;
throw { message: "error" };

const err = new Error();
throw "Error: " + err;
throw `${err}`;

Example of correct code:

throw new Error("error");
throw new TypeError("type error");

const err = new Error("error");
throw err;

class CustomError extends Error {
  constructor(message) {
    super(message);
  }
}
throw new CustomError("custom error");