Rewrite `throw "Err"` ⇒ `throw new Error("Err")`

JavaScript pattern

It is a good practice to throw Error objects on exceptions because they automatically keep track of where they were created.


Apply with the Grit CLI
grit apply no_throw_literal

String literal ⇒ new Error('...');

BEFORE
throw "error";
AFTER
throw new Error("error");

String concatenation ⇒ new Error('...')

BEFORE
throw "next " + "error";
AFTER
throw new Error("next " + "error");

String variable ⇒ new Error('...')

JAVASCRIPT
/* Wait for type analysis: https://github.com/iuvoai/rules/issues/200 */
/*var error = "Catch error!"
throw error;
// Wait for type analysis
var error = "Catch error!"
throw new Error(error);*/

Number ⇒ new Error(...)

BEFORE
throw 0;
AFTER
throw new Error(0);

undefinednew Error(undefined)

BEFORE
throw undefined;
AFTER
throw new Error(undefined);

nullnew Error(null)

BEFORE
throw null;
AFTER
throw new Error(null);

Do not change Error object without arguments

JAVASCRIPT
throw new Error();

Do not change Error object

JAVASCRIPT
try {
  throw new Error("error");
} catch (ex) {
  log(ex);
}

Do not change variables which assigned with Error object

JAVASCRIPT
var e = new Error("error");
throw e;