@altizure/errorcode
Advanced tools
+58
-1
| const en = require('./i18n/en/index.json') | ||
| const cn = require('./i18n/zh-cn/index.json') | ||
| const tw = require('./i18n/zh-tw/index.json') | ||
| // webpack will help inject this nodejs module | ||
| const util = require('util') | ||
@@ -8,5 +10,60 @@ const expo = { | ||
| 'zh-cn': cn, | ||
| 'zh-tw': tw | ||
| 'zh-tw': tw, | ||
| init: (props) => new ErrorManager(props) | ||
| } | ||
| class ErrorManager { | ||
| constructor(props) { | ||
| this.validate = this._validate.bind(this) | ||
| this.getDesc = this._getDesc.bind(this) | ||
| this.getSol = this._getSol.bind(this) | ||
| this.getCode = this._getCode.bind(this) | ||
| const { lang = 'en', descArgs = [], solArgs = [], code = [] } = props | ||
| this.lang = lang | ||
| this.descArgs = descArgs | ||
| this.solArgs = solArgs | ||
| this.code = code | ||
| this.v2 = this.validate() | ||
| this.desc = expo[this.lang][this.code]['Description'] | ||
| this.sol = expo[this.lang][this.code]['Solution'] | ||
| this.code = expo[this.lang][this.code]['ErrorNumber'] | ||
| } | ||
| _getDesc () { | ||
| if (this.v2) { | ||
| return util.format(this.desc, ...this.descArgs) | ||
| } else { | ||
| return this.desc | ||
| } | ||
| } | ||
| _getSol () { | ||
| if (this.v2) { | ||
| return util.format(this.sol, ...this.solArgs) | ||
| } else { | ||
| return this.sol | ||
| } | ||
| } | ||
| _getCode () { | ||
| return this.code | ||
| } | ||
| _validate () { | ||
| if (!['en', 'zh-cn', 'zh-tw'].includes(this.lang)) { | ||
| throw new Error(`unsupported lang ${this.lang}`) | ||
| } | ||
| if (!expo[this.lang][this.code]) { | ||
| throw new Error(`no such error code ${this.code} in ${this.lang}`) | ||
| } | ||
| if (this.descArgs.length === 0 && this.solArgs.length === 0) { | ||
| console.warn(`no descArgs nor solArgs defined. v1 mode for ${this.code}`) | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
| } | ||
| module.exports = expo |
+1
-1
| { | ||
| "name": "@altizure/errorcode", | ||
| "version": "1.0.22", | ||
| "version": "2.0.0", | ||
| "description": "i18n of error code", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
110437
1.35%2341
2.18%