{"_id":"module-error","_rev":"4212028","name":"module-error","description":"Create errors with code and cause properties","dist-tags":{"latest":"1.0.2"},"maintainers":[{"name":"vweevers","email":""}],"time":{"modified":"2026-03-04T17:06:46.000Z","created":"2021-10-22T13:37:02.421Z","1.0.2":"2022-02-19T15:42:41.644Z","1.0.1":"2021-10-24T20:49:22.652Z","1.0.0":"2021-10-22T13:37:02.421Z"},"users":{},"author":{"name":"Vincent Weevers"},"repository":{"type":"git","url":"git+https://github.com/vweevers/module-error.git"},"versions":{"1.0.2":{"name":"module-error","version":"1.0.2","description":"Create errors with code and cause properties","license":"MIT","author":{"name":"Vincent Weevers"},"scripts":{"test":"standard && tsc && hallmark && node test","build":"tsc -d --emitDeclarationOnly --noEmit false"},"types":"./index.d.ts","devDependencies":{"@types/node":"^17.0.6","@voxpelli/tsconfig":"^3.0.0","hallmark":"^4.0.0","standard":"^16.0.4","tape":"^5.3.1","typescript":"^4.4.4"},"keywords":["cause","code","error"],"repository":{"type":"git","url":"git+https://github.com/vweevers/module-error.git"},"bugs":{"url":"https://github.com/vweevers/module-error/issues"},"homepage":"https://github.com/vweevers/module-error","engines":{"node":">=10"},"gitHead":"bcbdc3614fdf1e75e94032671fad893cabf796a4","_id":"module-error@1.0.2","_nodeVersion":"16.9.1","_npmVersion":"7.21.1","dist":{"shasum":"8d1a48897ca883f47a45816d4fb3e3c6ba404d86","size":3394,"noattachment":false,"key":"/module-error/-/module-error-1.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/module-error/download/module-error-1.0.2.tgz"},"_npmUser":{"name":"vweevers","email":"dev@vincentweevers.nl"},"directories":{},"maintainers":[{"name":"vweevers","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/module-error_1.0.2_1645285361491_0.370455362840016"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-02-19T15:42:45.385Z","publish_time":1645285361644,"_cnpm_publish_time":1645285361644},"1.0.1":{"name":"module-error","version":"1.0.1","description":"Create errors with code and cause properties","license":"MIT","author":{"name":"Vincent Weevers"},"scripts":{"test":"standard && tsc && hallmark && node test","build":"tsc -d --emitDeclarationOnly --noEmit false"},"types":"./index.d.ts","devDependencies":{"@types/node":"^16.11.3","@voxpelli/tsconfig":"^3.0.0","hallmark":"^3.1.0","standard":"^16.0.4","tape":"^5.3.1","typescript":"^4.4.4"},"keywords":["cause","code","error"],"repository":{"type":"git","url":"git+https://github.com/vweevers/module-error.git"},"bugs":{"url":"https://github.com/vweevers/module-error/issues"},"homepage":"https://github.com/vweevers/module-error","engines":{"node":">=10"},"gitHead":"9cc6c8304a39c21954d2f13a530f99361b207d10","_id":"module-error@1.0.1","_nodeVersion":"16.9.1","_npmVersion":"7.21.1","dist":{"shasum":"761dc56831e2fabe6d9f780bbe4980a0b4ca2331","size":3347,"noattachment":false,"key":"/module-error/-/module-error-1.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/module-error/download/module-error-1.0.1.tgz"},"_npmUser":{"name":"vweevers","email":"dev@vincentweevers.nl"},"directories":{},"maintainers":[{"name":"vweevers","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/module-error_1.0.1_1635108562506_0.27540223262218677"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2021-12-28T00:01:21.551Z","publish_time":1635108562652,"_cnpm_publish_time":1635108562652},"1.0.0":{"name":"module-error","version":"1.0.0","description":"Create errors with code and cause properties","license":"MIT","author":{"name":"Vincent Weevers"},"scripts":{"test":"standard && tsc && hallmark && node test"},"devDependencies":{"@types/node":"^16.11.3","@voxpelli/tsconfig":"^3.0.0","hallmark":"^3.1.0","standard":"^16.0.4","tape":"^5.3.1","typescript":"^4.4.4"},"keywords":["cause","code","error"],"repository":{"type":"git","url":"git+https://github.com/vweevers/module-error.git"},"bugs":{"url":"https://github.com/vweevers/module-error/issues"},"homepage":"https://github.com/vweevers/module-error","engines":{"node":">=10"},"gitHead":"fae7ba26422960c9cba0ab6ebe8bd7f60d13ee3c","_id":"module-error@1.0.0","_nodeVersion":"16.9.1","_npmVersion":"7.21.1","dist":{"shasum":"4084a0db2a84b26ae123a35c836b24bde59de629","size":3134,"noattachment":false,"key":"/module-error/-/module-error-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/module-error/download/module-error-1.0.0.tgz"},"_npmUser":{"name":"vweevers","email":"dev@vincentweevers.nl"},"directories":{},"maintainers":[{"name":"vweevers","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/module-error_1.0.0_1634909822193_0.7919414773013909"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2021-12-28T00:01:20.651Z","publish_time":1634909822421,"_cnpm_publish_time":1634909822421}},"readme":"# module-error\n\n**Create errors with `code` and `cause` properties. Simple and extensible.**\n\n[![npm status](http://img.shields.io/npm/v/module-error.svg)](https://www.npmjs.org/package/module-error)\n[![node](https://img.shields.io/node/v/module-error.svg)](https://www.npmjs.org/package/module-error)\n[![Test](https://img.shields.io/github/workflow/status/vweevers/module-error/Test?label=test)](https://github.com/vweevers/module-error/actions/workflows/test.yml)\n[![Standard](https://img.shields.io/badge/standard-informational?logo=javascript&logoColor=fff)](https://standardjs.com)\n[![Common Changelog](https://common-changelog.org/badge.svg)](https://common-changelog.org)\n\n## Usage\n\nWorks like a regular `Error` constructor but adds an options argument (as [`proposal-error-cause`](https://github.com/tc39/proposal-error-cause) does).\n\n```js\nconst ModuleError = require('module-error')\n\nthrow new ModuleError('Message goes here', {\n  code: 'EXAMPLE_NOT_FOUND'\n})\n```\n\nThe primary purpose of `ModuleError` is to define a `code` property on the error object following Node.js conventions. It should be set to an uppercase string that uniquely identifies a situation, prefixed with the name of your module (or a collection of modules) to prevent conflicts.\n\nThe output looks like this in Node.js (some stack frames omitted for brevity):\n\n```\nModuleError: Message goes here\n    at Object.<anonymous> (/home/app/example.js:5:7)\n    at node:internal/main/run_main_module:17:47 {\n  code: 'EXAMPLE_NOT_FOUND'\n}\n```\n\nThe benefit of error codes is that messages can be changed without a semver-major release because your [semver](https://semver.org) contract will be on the codes. Codes can be reused across related modules while allowing individual modules to customize messages. I also prefer it over `instanceof MyError` logic because codes work cross-realm and when a tree of `node_modules` contains multiple versions of a module.\n\nTo wrap another error:\n\n```js\ntry {\n  JSON.parse(await fs.readFile('state.json'))\n} catch (err) {\n  throw new ModuleError('Could not load state', {\n    code: 'EXAMPLE_INVALID_STATE',\n    cause: err\n  })\n}\n```\n\nIf for convenience you want to create subclasses with prepared codes:\n\n```js\nclass NotFoundError extends ModuleError {\n  constructor(message, options) {\n    super(message, { ...options, code: 'EXAMPLE_NOT_FOUND' })\n  }\n}\n```\n\nThen you can do:\n\n```js\nthrow new NotFoundError('Message goes here')\n```\n\nUnder Node.js the stack trace will be adjusted accordingly, to skip the frame containing your error constructor.\n\n## API\n\n### `ModuleError(message, [options])`\n\nConstructor to create an error with the provided `message` string. Options:\n\n- `code` (string): if provided, define a `code` property with this value.\n- `cause` (Error): if provided, define a `cause` property with this value. Unlike the spec of [`proposal-error-cause`](https://github.com/tc39/proposal-error-cause) the property is enumerable so that Node.js (v16 at the time of writing) will print it. Firefox prints it regardless, Chromium doesn't yet.\n- `expected`: if truthy, define a `expected` property with value `true`. This is useful for command line interfaces to differentiate unexpected errors from e.g. invalid user input. A pattern I like to follow is to print only an error message if `err.expected` is true and no `--verbose` flag was provided. Otherwise print the full stack.\n- `transient`: if truthy, define a `transient` property with value `true`. This communicates to users that the operation that caused the error may be retried. See also [`transient-error`](https://github.com/vweevers/transient-error) to mark existing errors as transient.\n\n## Install\n\nWith [npm](https://npmjs.org) do:\n\n```\nnpm install module-error\n```\n\n## License\n\n[MIT](LICENSE)\n","_attachments":{},"homepage":"https://github.com/vweevers/module-error","bugs":{"url":"https://github.com/vweevers/module-error/issues"},"license":"MIT"}