{"_id":"@airquality/custom-error","_rev":"3-3e1183a1216b55fa33de48cfdf100d7c","name":"@airquality/custom-error","description":"Custom error","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@airquality/custom-error","version":"1.0.0","description":"Custom error","main":"index.js","scripts":{"test":"mocha"},"repository":{"type":"git","url":"git+https://github.com/LeeWeisheng/custom-error.git"},"keywords":["error","custom","node"],"author":{"name":"李蔚生","email":"971353677@qq.com"},"license":"MIT","bugs":{"url":"https://github.com/LeeWeisheng/custom-error/issues"},"homepage":"https://github.com/LeeWeisheng/custom-error#readme","devDependencies":{"eslint":"^4.11.0","eslint-config-standard":"^10.2.1","eslint-plugin-import":"^2.8.0","eslint-plugin-node":"^5.2.1","eslint-plugin-promise":"^3.6.0","eslint-plugin-standard":"^3.0.1","mocha":"^4.0.1"},"dependencies":{"clone":"^2.1.1","es6-template-strings":"^2.0.1","is-plain-object":"^2.0.4"},"gitHead":"84b38fa7348f43738827784298242ea57f120643","_id":"@airquality/custom-error@1.0.0","_npmVersion":"5.5.1","_nodeVersion":"9.0.0","_npmUser":{"name":"airquality","email":"971353677@qq.com"},"dist":{"integrity":"sha512-TQ/AXI0/Ytz2VoMNIqpzvBDoMz6B7nhYys2dCAX8olqb41Q/TkZ2WPZ1axEhiRBVrLj5tPbIBtrlHog0q9owmA==","shasum":"c36ec21d4a052c89fba837c9210462adced75f9a","tarball":"https://registry.npmjs.org/@airquality/custom-error/-/custom-error-1.0.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDC8qKTPRuv1b3MehzAWGqwhkm9qCWeHjbSKSqW2uDoQAiBkrsfZWkndDZhjIDqsP1+7kp3S+5Wdy6Ypy19shhZxgQ=="}]},"maintainers":[{"name":"airquality","email":"971353677@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/custom-error-1.0.0.tgz_1510855274784_0.929156249621883"},"directories":{}}},"readme":"# @airquality/custom-error\n\n自定义错误。可统一设置错误码、错误信息和用户自定义属性。\n\n## 安装\n\n```sh\nnpm i --save @airquality/custom-error\n```\n\n## 使用\n\n此模块导出一个 `CustomError` 构造器和一个 `createError` 函数。\n\n### `CustomError`\n\n- `new CustomError([code[, params]])` 实例化\n- `CustomError.set(errors)` 配置错误信息\n- `CustomError.set(code, message)` 配置错误信息\n- `CustomError.set(code, properties)` 配置错误信息\n- `CustomError.get([code])` 得到错误信息\n\n在实例化之前，应该先配置统一的错误信息，方法维护。有三种方法可以配置错误信息。\n\n```js\nconst CustomError = require('@airquality/custom-error')\n\n// 1. 通过一个错误信息对象数组进行配置，每个对象中都必须包含 code 属性，\n//    message 属性是可选的，它可以是一个字符串模板，在实例化时可通过传\n//    入 params 参数编译成最终展示的信息。\nCustomError.set([\n\n  // code 属性是必须的\n  { code: 'ERR_CUSTOM_ERROR1', message: 'message' },\n\n  // 可传入字符串模板\n  { code: 'ERR_CUSTOM_ERROR2', message: 'error message ${message}' },\n\n  // 可忽略 message 参数，并添加用户自定义的属性\n  { code: 'ERR_CUSTOM_ERROR3', otherProp: 'other prop' },\n])\n\n// 2. 通过传入 code 和 message 字符串进行配置\nCustomError.set('ERR_CUSTOM_ERROR4', 'error message')\n\n// 3. 通过传入 code 字符串和 props 对象进行配置\nCustomError.set('ERR_CUSTOM_ERROR5', {\n  message: 'error message',\n  otherProp: 'other prop',\n})\n```\n\n配置错误信息后，可通过 `get` 方法得到这些信息。\n\n```js\n// 根据 code 得到某一条错误信息\n// => { code: 'ERR_CUSTOM_ERROR1', message: 'message' }\nCustomError.get('ERR_CUSTOM_ERROR1')\n\n// 不传入 code 参数，可得到全部的错误信息（数组）\nCustomError.get()\n```\n\n实例化。\n\n```js\n// 不传入任何的参数\nconst customError1 = new CustomError()\n\n// 传入已经配置的 code，输出的错误信息时 code 对应的 message\nconst customError2 = new CustomError('ERR_CUSTOM_ERROR1')\n\n// 传入的 code 如果没有在配置中，则输出的错误信息就是 code\nconst customError3 = new CustomError('Non-existent code will be treated as message')\n\n// 可传入 params 参数，解析字符串模板\n// 下面输出的错误信息应该是：'error message test'\nconst customError4 = new CustomError('ERR_CUSTOM_ERROR2', { message: 'test' })\n```\n\n### `createError(name[, errors])`\n\n返回一个构造器，这个构造器的实例的名称前缀为用户输入的 `name` 参数。传入 `errors` 数组，可配置错误信息，其具体使用方法与 `CustomError.set(errors)` 相同。关于构造器的使用方法，与上面的 `CustomError` 相同。\n\n```js\nconst { createError } = require('@airquality/custom-error')\nconst UserError = createError('UserError', [\n  { code: 'ERR_CUSTOM_ERROR', message: 'message' },\n])\n\n// ...\n```\n\n## 许可\n\nMIT.\n","maintainers":[{"email":"npm@npmjs.com","name":"npm"}],"time":{"modified":"2022-04-04T12:23:09.016Z","created":"2017-11-16T18:01:14.870Z","1.0.0":"2017-11-16T18:01:14.870Z"},"homepage":"https://github.com/LeeWeisheng/custom-error#readme","keywords":["error","custom","node"],"repository":{"type":"git","url":"git+https://github.com/LeeWeisheng/custom-error.git"},"author":{"name":"李蔚生","email":"971353677@qq.com"},"bugs":{"url":"https://github.com/LeeWeisheng/custom-error/issues"},"license":"MIT","readmeFilename":"README.md"}