Skip to main content

isError()

isError(value): value is Error

Checks if a value is an Error instance.

note

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')checkmark true
new TypeError('msg')checkmark true
new RangeError('msg')checkmark true
new SyntaxError('msg')checkmark true
{ message: 'error' }cross false
'Error'cross 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);
}
}