The `custom-error-instance` library provides a robust mechanism for creating custom JavaScript error constructors that correctly resolve with `instanceof` checks, including support for inheritance hierarchies. It seamlessly integrates with Node.js's native `Error` implementation, allowing developers to extend and define distinct error types without directly modifying the global `Error` object. Key features include the ability to attach custom properties, control error output, and specify stack trace lengths, addressing common challenges when creating custom error classes. The package is currently at version 2.1.2 (as of late 2023) and appears to be in a maintenance phase, with updates occurring on an as-needed basis rather than a fixed release cadence. Its primary differentiator lies in ensuring reliable `instanceof` behavior for custom errors, which can be tricky to implement correctly when extending the native `Error` class manually.
npm install custom-error-instanceVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to define a base custom error, create inheriting child errors with specific properties (like a 'code'), and use `instanceof` checks to handle different error types within a `try...catch` block. It simulates adding items to a store with potential key conflicts or random errors.
For explicit CJS usage, stick to `require`. For ESM, `import CustomError from 'custom-error-instance';` is generally the correct approach for default exports from CJS. Avoid named imports like `import { CustomError } from 'custom-error-instance';` as they are unlikely to work.Review the project's specific needs regarding new JS features or comprehensive TS types. For the core functionality of custom `instanceof` errors, it remains effective. Consider contributing typings if needed for TypeScript projects.
Ensure `CustomError` is imported correctly for CommonJS: `const CustomError = require('custom-error-instance');` or for ESM interoperability: `import CustomError from 'custom-error-instance';`.Verify that `CustomError` itself is correctly imported and that sub-errors like `CustomError.inuse` have been properly defined using `CustomError('ChildError', ParentError, ...)` before instantiation.No dependency data recorded yet.