Error Comparison and Information Utility
check-error is a JavaScript utility library designed for robust error inspection and comparison in both Node.js and browser environments. Currently at stable version 2.1.3, it provides functions to retrieve error information, such as constructor names and messages, and to determine compatibility between errors based on their instance, constructor, or message content. It is a foundational utility often employed within testing frameworks, particularly Chai, for asserting error conditions. Since version 2.0.0, it is an ESM-only module, requiring modern JavaScript environments (Node.js 16+, Firefox 102+, Chromium Edge+). Releases are infrequent, focusing on maintenance and modernizing the codebase to align with current JavaScript standards. Its key differentiator is providing a standardized set of primitives for error interrogation, abstracted away from environment-specific quirks, which simplifies error handling logic across different contexts. This ensures consistent behavior when dealing with various types of errors and their properties.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` the `check-error` package in a CommonJS environment after upgrading to v2.0.0 or later.fixChange `const checkError = require('check-error');` to `import * as checkError from 'check-error';` and ensure your project is configured for ESM (e.g., `"type": "module"` in package.json). -
TypeError: checkError.compatibleMessage is not a function
cause This error is unlikely for `compatibleMessage` specifically, but if a function from the `checkError` object is not found, it implies an incorrect import or an attempt to use a non-existent method.fixVerify that `checkError` is correctly imported as `import * as checkError from 'check-error';` and that the method name (`compatibleMessage` or others) is spelled correctly and exists in the current version of the library.
Warnings
- breaking Version 2.0.0 introduced a breaking change, converting the module to ESM-only. CommonJS `require()` is no longer supported.
- breaking With the shift to ESM-only in v2.0.0, support for older environments including Node.js versions below 16, Firefox versions less than 102, and all versions of Internet Explorer and older Edge (v18 and below) was dropped.
- gotcha Prior to v2.1.2, the `compatibleMessage` function might not correctly handle invalid inputs, potentially leading to unexpected behavior or errors when comparing messages.
Install
-
npm install check-error -
yarn add check-error -
pnpm add check-error
Imports
- checkError
const checkError = require('check-error');import * as checkError from 'check-error';
- compatibleConstructor, getMessage
import { compatibleConstructor, getMessage } from 'check-error';
Quickstart
import * as checkError from 'check-error';
class CustomError extends Error {
constructor(message: string) {
super(message);
this.name = 'CustomError';
}
}
function potentiallyFailingOperation(shouldThrow: boolean, type: string = 'TypeError'): void {
if (shouldThrow) {
if (type === 'CustomError') {
throw new CustomError('Something went wrong with custom logic.');
} else {
throw new TypeError('Invalid data format received.');
}
}
}
try {
potentiallyFailingOperation(true);
} catch (e: unknown) {
if (e instanceof Error) {
console.log(`Caught error: ${e.message}`);
console.log(`Constructor name: ${checkError.getConstructorName(e)}`);
console.log(`Is compatible with TypeError? ${checkError.compatibleConstructor(e, TypeError)}`);
console.log(`Is compatible with message 'data format'? ${checkError.compatibleMessage(e, 'data format')}`);
console.log(`Is compatible with CustomError? ${checkError.compatibleConstructor(e, CustomError)}`);
}
}
try {
potentiallyFailingOperation(true, 'CustomError');
} catch (e: unknown) {
if (e instanceof Error) {
console.log(`\nCaught another error: ${e.message}`);
console.log(`Constructor name: ${checkError.getConstructorName(e)}`);
console.log(`Is compatible with TypeError? ${checkError.compatibleConstructor(e, TypeError)}`);
console.log(`Is compatible with CustomError? ${checkError.compatibleConstructor(e, CustomError)}`);
}
}