{"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":{"integrity":"sha512-0yuvsqSCv8LbaOKhnsQ/T5JhyFlCYLPXK3U2sgV10zoKQwzs/MyfuQUOZQ1V/6OCOJsK/TRgNVrPuPDqtdMFtA==","shasum":"8d1a48897ca883f47a45816d4fb3e3c6ba404d86","tarball":"http://registry-npm.ywwl.com/module-error/download/module-error-1.0.2.tgz","fileCount":6,"unpackedSize":7281,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiEQ/xACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoW1w//YuVGBpfBCEmQJJEVVdcm9Vqiz1PQGrOe+TD13OKxQ2vaS//h\r\nLW1myi6SkkweWarnN7ZiVHUF8KpS285KwiazEcEsm4Yv75xP4jbTurmPSfx0\r\nXX9GvseZsFE92EbMdgAwF77WrAPEPbX1STUWBFPRaNkFlEH0bjwJXC9xqW08\r\n2FTf9B/nYp3NSYTgMW3QYqoRCVJjKDK4t4kkU0iIb036gP00TamOTHeMplP2\r\njKQDAwDlmXRPNzWGXCjkadK+rcBPOogn7MauXtlpH5i9VGIogsFYq1+z+ySI\r\nD13SWWKyrwJLpPWhYZ3NeXMehAMsYcakSppCJO4wjPWRCY6PKSnoFHKXV823\r\nb3rHnV5VTrDucPsVaCSmphb+w17iOZxR50Sgd0PO+GA+T9tL3q69TU7q+ahR\r\nX0hTQN5w4q4G6k1JuGaEjefGKu4D9vwpitjsejA2CvzToLTeu3FQeTXy47C1\r\n+8gY2mFELMkqxu2oPAAj3KnzywE4LNBp33TP135dFeO1IdMDepNk15vYThPB\r\ndu+vPpB95GclcclBPgPra29H9nCti0QlAnmKJ0h8BnZZJdOUyDtEroFEUMVb\r\nErWtwDVHftAvmQTz2QJAuPCaXexAZOG3oB/ez/5JGAGcDY/Qd/wCJ2VOseXS\r\nTzWbYG3NitVJQX1+SHE7Tj6m//pvpuhUNdU=\r\n=X8kw\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCaltibYeodcZ5ZV6I5/8D2SmZuwLT6kHjYxy8uceFyXAIhAM+R4AoJYoi0frA8Sw4TXz7Inf2KQGjNmQ3qo7VdktZ+"}],"size":3394,"noattachment":false,"key":"/module-error/-/module-error-1.0.2.tgz"},"_npmUser":{"name":"vweevers","email":"dev@vincentweevers.nl"},"directories":{},"maintainers":[{"name":"vweevers","email":"dev@vincentweevers.nl"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/module-error_1.0.2_1645285361491_0.370455362840016"},"_hasShrinkwrap":false,"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","publish_time":1645285361644,"_cnpm_publish_time":1645285361644,"dist-tags":{"latest":"1.0.2"}}