Skip to content

Latest commit

Β 

History

History
51 lines (34 loc) Β· 1.4 KB

File metadata and controls

51 lines (34 loc) Β· 1.4 KB

prefer-error-is-error

πŸ“ Prefer Error.isError() when checking for errors.

🚫 This rule is disabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

Error.isError() is a robust way to check whether a value is an error object.

instanceof Error is unreliable across realms, and Object.prototype.toString.call(error) === '[object Error]' can be spoofed with Symbol.toStringTag.

Unlike unicorn/no-instanceof-builtins, this rule also catches legacy Error brand checks using '[object Error]'.

Examples

// ❌
error instanceof Error;

// βœ…
Error.isError(error);
// ❌
Object.prototype.toString.call(error) === '[object Error]';

// βœ…
Error.isError(error);
// ❌
Object.prototype.toString.call(error) !== '[object Error]';

// βœ…
!Error.isError(error);
// βœ…
error instanceof TypeError;

Related rules