Aller au contenu principal

isError()

isError(value): value is Error

Checks if a value is an Error instance.

remarque

Also returns true for Error subclasses (TypeError, RangeError, custom errors).


Parameters

value: unknown

The value to check.


Returns: value is Error

true if the value is an Error, false otherwise.


Since

2.0.0


Example

isError(new Error('oops'));     // => true
isError(new TypeError('bad')); // => true
isError({ message: 'fake' }); // => false
isError('error string'); // => false

How it works?

Type guard that checks if a value is an Error instance.

Type Narrowing

Common Checks

ValueResult
new Error('msg') true
new TypeError('msg') true
new RangeError('msg') true
new SyntaxError('msg') true
{ message: 'error' } false
'Error' false

Use Cases

Catch exceptions correctly 📌

Distinguish between standard Error objects and other thrown values. Critical for robust error handling and logging.

try {
dangerousCall();
} catch (err) {
if (isError(err)) {
logError(err.message, err.stack);
}
}