throwErrors
Reports throwing values that are not
Errorobjects.
✅ This rule is included in the ts logical presets.
Only Error objects (or objects that extend Error) should be thrown.
Throwing non-Error values has several downsides:
- No stack trace information for debugging
- Inconsistent error handling behavior
- Cannot use standard error properties like
cause,message, orname
This rule reports on any throw statement where the value being thrown is not an Error.
Examples
Section titled “Examples”throw "Something went wrong";throw { message: "error", code: 500 };throw new Error("Something went wrong");class HttpError extends Error { constructor( message: string, public code: number, ) { super(message); this.name = "HttpError"; }}throw new HttpError("error", 500);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you’re working with legacy code that throws non-Error values and refactoring would be too costly, you may disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.