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: string
message: "error", code: number
code: 500 };throw new var Error: ErrorConstructornew (message?: string) => Error
Error("Something went wrong");class class HttpError
HttpError extends var Error: ErrorConstructor
Error { constructor( message: string
message: string, public HttpError.code: number
code: number, ) { super(message: string
message); this.Error.name: string
name = "HttpError"; }}throw new constructor HttpError(message: string, code: number): HttpError
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 ❤️🔥 around the world by
the Flint team and contributors.