{"_id":"@b2b-alive/ts-error-generator","_rev":"5-ccd5a16c837c5a6ae45a1a0a828ddab1","time":{"1.0.0":"2018-11-13T13:13:26.755Z","created":"2018-11-13T13:14:20.791Z","1.0.1":"2018-11-13T13:14:21.072Z","modified":"2022-04-04T17:24:02.982Z","1.1.0":"2018-11-30T18:21:43.992Z"},"name":"@b2b-alive/ts-error-generator","dist-tags":{"latest":"1.1.0"},"versions":{"1.0.1":{"name":"@b2b-alive/ts-error-generator","version":"1.0.1","description":"[![Build Status](https://travis-ci.org/b2b-alive-ltd/ts-error-generator.svg?branch=master)](https://travis-ci.org/b2b-alive-ltd/ts-error-generator)\r [![Coverage Status](https://coveralls.io/repos/github/b2b-alive-ltd/ts-error-generator/badge.svg?branch=ma","main":"dist/index.js","types":"dist/index.d.ts","scripts":{"watch":"tsc -w","build":"tsc","test":"mocha --reporter spec","cover":"nyc mocha"},"keywords":["typescript","error","custom"],"author":{"name":"Florent Quillien","email":"florent.quillien@b2b-alive.com"},"license":"ISC","repository":{"type":"git","url":"git+https://github.com/b2b-alive-ltd/ts-error-generator.git"},"bugs":{"url":"https://github.com/b2b-alive-ltd/ts-error-generator"},"homepage":"https://github.com/b2b-alive-ltd/ts-error-generator#readme","devDependencies":{"@types/node":"^10.12.5","chai":"^4.2.0","coveralls":"^3.0.2","mocha":"^5.2.0","nyc":"^13.1.0","standard":"^12.0.1","typescript":"^3.1.6"},"nyc":{"include":["dist/**/*"],"extension":[".js"],"reporter":["text","lcov"],"all":true},"gitHead":"85a8486a4f7b9354fec9ab3d58f2e8f55aa1d712","_id":"@b2b-alive/ts-error-generator@1.0.1","_npmVersion":"6.4.1","_nodeVersion":"10.12.0","_npmUser":{"name":"b2b-dev","email":"florent.quillien@b2b-alive.com"},"dist":{"integrity":"sha512-Dn8NQHLxB2WLfcOD2u3E95SpQhFyv2zP0yVYvvibR7iY5gg2LjlRjCPrq3ESsF7Aqt/IWHJU47vOOvikQXWC8g==","shasum":"25e69ff44068277d37591f9f3f110e21163f4417","tarball":"https://registry.npmjs.org/@b2b-alive/ts-error-generator/-/ts-error-generator-1.0.1.tgz","fileCount":9,"unpackedSize":29524,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJb6s4tCRA9TVsSAnZWagAAeCsP/iFWV77yPrVlBuyULJFG\nlGyoMd65cahVcaEB/uKZyQzGa+l/3GXVNzcm5nvS0r4tn48wwOyRPDlycvFu\nR3cFk40f3I4zWFMr5hVYV1Nrd1xdWmZa+zCUARf4T1pcT6FaFcGCClvmq17P\nUERaR00HkuBVnKqgxWdpXxPIG5mbTBunCLwOWCw7fS+SE+ZSIeGdyHweDEX5\nLxby6sfcYLNAl7Kp7UMG9mp5CjBWCS9UKIyhh3lm5UNC2WyZpktG4Bhr6en8\nMapL7WanRRSpFGB2rjvN+dk7iW+b3hEBNgD0MPacMktSeLmrJAaEgp8aHoUM\nf6ZZWUEaWtxcc2Hsv3CInKO7VzuNx9nWj3+kEDAf3HG4hCcpQ6Ba0bmv/e7+\n3sM2q/BTO/cslshuG/7vzEG+VqwEpCaXJpTlSEuwywsF6+Xiit3dFtPHx9qI\nzc91T8tmlcd1xB3BeMYQHZGhwk4mhPtBYEZEUOu/xEzzerIw4bgKCVMqBAYn\n7+CEefp9YbTbBAmRXDmPIiN+yHZM0Oev8FNINMIKINsYw7s6BhifT8IUbNvg\npXSN7x+CMbxeU9vAVB4HwxukiOP7DpgYIeIQqqEVNL3KrLm1JUORA/48hafo\nDGp05JxnOZPSqGyRrzCMrcKaCGUq4gkv0WAaw2PZ6i+h2RGNCr1vLCBhbMxF\n38Lb\r\n=jqFR\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCmf7ghbKFiik9KKXkTkUX1IXOPFTiuZa31Mec+cCqDfAIhAO00Xdf+1K6spd9PevLZjNeVBaa+NKyH78gq5nQv8BbX"}]},"maintainers":[{"name":"b2b-dev","email":"florent.quillien@b2b-alive.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ts-error-generator_1.0.1_1542114860957_0.31681944595261413"},"_hasShrinkwrap":false},"1.1.0":{"name":"@b2b-alive/ts-error-generator","version":"1.1.0","description":"[![Build Status](https://travis-ci.org/b2b-alive-ltd/ts-error-generator.svg?branch=master)](https://travis-ci.org/b2b-alive-ltd/ts-error-generator)\r [![Coverage Status](https://coveralls.io/repos/github/b2b-alive-ltd/ts-error-generator/badge.svg?branch=ma","main":"dist/index.js","types":"dist/index.d.ts","scripts":{"watch":"tsc -w","build":"tsc","test":"mocha --reporter spec","cover":"nyc mocha"},"keywords":["typescript","error","custom"],"author":{"name":"Florent Quillien","email":"florent.quillien@b2b-alive.com"},"license":"ISC","repository":{"type":"git","url":"git+https://github.com/b2b-alive-ltd/ts-error-generator.git"},"bugs":{"url":"https://github.com/b2b-alive-ltd/ts-error-generator"},"homepage":"https://github.com/b2b-alive-ltd/ts-error-generator#readme","devDependencies":{"@types/node":"^10.12.5","chai":"^4.2.0","coveralls":"^3.0.2","mocha":"^5.2.0","nyc":"^13.1.0","standard":"^12.0.1","typescript":"^3.1.6"},"nyc":{"include":["dist/**/*"],"extension":[".js"],"reporter":["text","lcov"],"all":true},"gitHead":"4a99177eba177ded4f80b915e5df64838d8f2d37","_id":"@b2b-alive/ts-error-generator@1.1.0","_npmVersion":"6.4.1","_nodeVersion":"10.12.0","_npmUser":{"name":"b2b-dev","email":"florent.quillien@b2b-alive.com"},"dist":{"integrity":"sha512-14X5zFnqHzNGP31Uy3f5J5Pd4yhhNcW/AH7XL4S/OaorYLdzve16MFL2pHgOSHbPr5EKZNjDfKOVWE69TKmAOg==","shasum":"3145df18e9007bee004037b6b19df6da933d6771","tarball":"https://registry.npmjs.org/@b2b-alive/ts-error-generator/-/ts-error-generator-1.1.0.tgz","fileCount":9,"unpackedSize":29681,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcAX+4CRA9TVsSAnZWagAAvdgP+QBzYA2p2BcaXmwWSWcb\nvjUKuOuUivB/w7q794xnXFxaa/U57auF5SvZaHcyo6TVpiOla6QRK1WxekGT\nZBuFtqgTblAFSdtjfe9cKrp7MjXrHwGs/oJT3tShNq04qGRWIZleewT3e2kh\noKmK/JnZX7Ewsy6DzFAyGVb23f/ZArHxjsxOzhefYxeu+Qlr5HR4VS3g+XBf\nW8nb3BrVxkppedIGr80+bau9SFnBa8gzkRCgVLpEETD6uVHpR/hRNMHUDgn1\nr0ht5b4xwwUw5oX3ZoT9Ymma+5RTQGTzKz6rU8p7GjxRxLYP21tGIQT+B7Ua\nzA7Ilo9XEE0eyRkXJoFOIs/doDHjkjjIi6nyQZgTJeELBc/GPsasKQjPEYKC\n7Jf4U7LYtCXeWL9aX34Jon4D+RWj+MI4ASKO/m0IXSEVvzNWgdfMiuROiuZY\nQmqUYXTJHvZ1PYhTedzzo03ruFgsDmJKSa8kNu4WI2ztHTN01gvXTewAN1nc\n5B5zlmKy76pHqkJr8LedE+Hwb2KsvHCha8mIHZ8ickvAtayotzYwBN31hb7F\nMo4k1pC2btbOJQfYi0CdY6oUTElQjzNJSDGZYa3U5VYbvI86FY1Karcq6/XX\ngJLQ7Zdfgn2A2kYamrJMKi70dL9cz8D5iOCCPT+VnMkZZd7jWe+cebR0q1XY\nIMvt\r\n=35PE\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICFuso1OcWG5RqHaQl2qGbpAPs6/Diiy2s80RjHnm9RAAiAYZOE9mYoiiTc6i3+lusaXTIdeCI40s55zBqfyNHgXsQ=="}]},"maintainers":[{"name":"b2b-dev","email":"florent.quillien@b2b-alive.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ts-error-generator_1.1.0_1543602103840_0.5134619663108659"},"_hasShrinkwrap":false}},"maintainers":[{"name":"b2b-dev","email":"florent.quillien@b2b-alive.com"}],"description":"[![Build Status](https://travis-ci.org/b2b-alive-ltd/ts-error-generator.svg?branch=master)](https://travis-ci.org/b2b-alive-ltd/ts-error-generator)\r [![Coverage Status](https://coveralls.io/repos/github/b2b-alive-ltd/ts-error-generator/badge.svg?branch=ma","homepage":"https://github.com/b2b-alive-ltd/ts-error-generator#readme","keywords":["typescript","error","custom"],"repository":{"type":"git","url":"git+https://github.com/b2b-alive-ltd/ts-error-generator.git"},"author":{"name":"Florent Quillien","email":"florent.quillien@b2b-alive.com"},"bugs":{"url":"https://github.com/b2b-alive-ltd/ts-error-generator"},"license":"ISC","readme":"[![Build Status](https://travis-ci.org/b2b-alive-ltd/ts-error-generator.svg?branch=master)](https://travis-ci.org/b2b-alive-ltd/ts-error-generator)\r\n[![Coverage Status](https://coveralls.io/repos/github/b2b-alive-ltd/ts-error-generator/badge.svg?branch=master)](https://coveralls.io/github/b2b-alive-ltd/ts-error-generator?branch=master)\r\n\r\n\r\n[![NPM](https://nodei.co/npm/@b2b-alive/ts-error-generato.png)](https://www.npmjs.com/package/@b2b-alive/ts-error-generator)\r\n\r\nts-error-generator\r\n===================\r\n\r\nCustom errors and exceptions with TypeScript interface support in Node.js\r\n\r\nThis project is inspired by node-custom-error https://github.com/jproulx/node-custom-error/\r\n\r\n\r\n\r\n## Install\r\n\r\n```bash\r\n$ npm install @b2b-alive/ts-error-generator --save\r\n```\r\n\r\n## Usage\r\n\r\n```typescript\r\nimport defineError, { ICustomError } from '@b2b-alive/ts-error-generator';\r\n\r\ninterface APIError extends ICustomError{\r\n    required: string\r\n}\r\n\r\ninterface HTTPError extends ICustomError{\r\n    code: number,\r\n    status: string\r\n}\r\n\r\nconst ValidationError = defineError<APIError>('ValidationError', { 'required' : 'Missing parameter x' }, TypeError);\r\nconst HTTPError = defineError<HTTPError>('HTTPError', { 'code' : 500, 'status' : 'Server Error' });\r\n\r\nthrow new ValidationError('Required');\r\n```\r\n\r\nThe generator function supports the following parameters:\r\n\r\n* `name` {String} (required) - A custom name for this error type, which is printed when logging and in stack traces\r\n\r\n* `data` {Object} (optional) - Additional properties to attach to the error, in key=value pairs or as object descriptors\r\n\r\n* `Constructor` {Function} (optional) - A function to inherit from. Allows for additional methods and properties to be defined on your custom errors\r\n\r\nThe errors created by the generated functions are identical to built-in Error objects, with additional features such as:\r\n\r\nCustom properties can be attached and accessed at run time:\r\n```typescript\r\nconst error = new HTTPError('Uh oh');\r\nconsole.log(error.code, error.status); // prints 500 Server Error\r\n```\r\n\r\n## Formatting\r\n\r\nSimilar to `console.log`, the custom error message will be formatted from all available string and number arguments, using `util.format`:\r\n```typescript\r\nconst error = new ValidationError('%s: %s', 'Missing Field', 'name');\r\nconsole.log(error); // prints ValidationError: Missing Field: name\r\n```\r\n\r\n## Wrapped errors\r\n\r\nOther error objects can be passed in as arguments, which augment the original error stack trace with their own stack traces:\r\n```typescript\r\nconst error = new ValidationError('Missing field');\r\nconst serverError = new HTTPError('Something went wrong', error);\r\nconsole.log(serverError.stack);\r\n```\r\noutputs:\r\n```bash\r\nHTTPError: Something went wrong\r\n    at Object.<anonymous> (/Projects/ts-error-generator/dist/test.js:10:21)\r\n    at Module._compile (internal/modules/cjs/loader.js:688:30)\r\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)\r\n    at Module.load (internal/modules/cjs/loader.js:598:32)\r\n    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)\r\n    at Function.Module._load (internal/modules/cjs/loader.js:529:3)\r\n    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)\r\n    at startup (internal/bootstrap/node.js:285:19)\r\n    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)\r\nValidationError: Missing field\r\n    at Object.<anonymous> (/Projects/ts-error-generator/dist/test.js:9:15)\r\n    at Module._compile (internal/modules/cjs/loader.js:688:30)\r\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)\r\n    at Module.load (internal/modules/cjs/loader.js:598:32)\r\n    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)\r\n    at Function.Module._load (internal/modules/cjs/loader.js:529:3)\r\n    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)\r\n    at startup (internal/bootstrap/node.js:285:19)\r\n    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)\r\n```\r\nMultiple error objects are allowed to be passed, and are processed in order.\r\n\r\n## JSON support\r\nErrors can also be serialized into JSON format by using `error#toJSON();`. This will enumerate all of the hidden and custom properties, and also format the stack trace into an array of individual lines:\r\n\r\n```typescript\r\nconsole.log(error.toJSON()); // or console.log(JSON.stringify(error));\r\n```\r\noutputs\r\n```bash\r\n{ stack:\r\n   [ 'ValidationError: Required',\r\n     '    at Object.<anonymous> (/Projects/ts-error-generator/dist/test.js:9:15)',\r\n     '    at Module._compile (internal/modules/cjs/loader.js:688:30)',\r\n     '    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)',\r\n     '    at Module.load (internal/modules/cjs/loader.js:598:32)',\r\n     '    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)',\r\n     '    at Function.Module._load (internal/modules/cjs/loader.js:529:3)',\r\n     '    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)',\r\n     '    at startup (internal/bootstrap/node.js:285:19)',\r\n     '    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)' ],\r\n  message: 'Required',\r\n  required: 'Missing parameter x' }\r\n```\r\n\r\n## Custom Constructor\r\nFinally, a custom function can be passed in as a 3rd argument. This will allow you to modify the custom error prototype without having to modify the original native Error prototype:\r\n\r\n```typescript\r\ninterface IAPIError extends ICustomError{\r\n    code: number,\r\n    status: string\r\n    env: string\r\n}\r\n\r\nconst APIError = defineError<IAPIError>('HTTPError', {env: process.env.NODE_ENV, route: '/'}, function (message: string, code: number) {\r\n    const http = require('http');\r\n    // Set custom properties when thrown based on additional arguments\r\n    this.code = code;\r\n    this.status = http.STATUS_CODES[code];\r\n    // We can override the default message logic if desired:\r\n    this.message = `${message} on ${this.route}`\r\n});\r\n\r\nconst error = new APIError('You do not have permission', 403);\r\nconsole.log(error.message, [error.code, error.status, error.env]);\r\n// result: HTTPError: HTTPError: You do not have permission on / [ 403, \"Forbidden\" , \"development\"]\r\n```\r\n\r\n## Notes\r\nCare is taken to preserve the built-in error handling behavior as much as possible, supporting:\r\n\r\n* `custom instanceOf Error`\r\n\r\n* `Error.prototype.isPrototypeOf(custom)`\r\n\r\n* `util.isError(custom)`\r\n\r\n* `custom = generator('message')`\r\n\r\n* `custom = new generator('message');`\r\n\r\nIn other words, you shouldn't have to worry about these errors affecting your syntax or existing code. Simply drop in place for any existing errors you're throwing and it should work just the same.","readmeFilename":"README.md"}