Rule: no-string-throw
Flags throwing plain strings or concatenations of strings.
Rationale
Example – Doing it right
// throwing an Error from typical function, whether sync or async
if (!productToAdd) {
throw new Error("How can I add new product when no value provided?");
}
Example – Anti Pattern
// throwing a string lacks any stack trace information and other important data properties
if (!productToAdd) {
throw ("How can I add new product when no value provided?");
}
Only Error objects contain a .stack
member equivalent to the current stack trace.
Primitives such as strings do not.
Notes:
Has Fixer
Config
Not configurable.
Config examples
"no-string-throw": true
Schema
null
Code examples:
Flags throwing plain strings or concatenations of strings.
"rules": { "no-string-throw": true }
Passes
try {
// ...
} catch (e) {
throw e;
}
Fails
try {
// ...
} catch {
throw 'There was a problem.';
}