update errorManager

This commit is contained in:
Jay
2017-06-04 11:54:25 +08:00
parent 2da1836d44
commit 9423428b72
5 changed files with 89 additions and 24 deletions
@@ -0,0 +1,35 @@
const fs = require('fs');
const path = require('path');
class errorManager {
constructor(){
this.errs = {};
this.defLang = 'zh';
let err = require(`./language/zh`);
this.errs['zh'] = err;
}
getMsg(code, lang = ''){
let lng = this.defLang;
if(typeof lang == 'string' && lang.trim().length > 0) {
let l = lang.split(',');
if(l.length > 0){
let tmp = l[0].substring(0, 2);
if(tmp.trim().length > 0) lng = tmp;
}
}
if(!(lng in this.errs)){
try{
let errs = require(`./language/${lng}`);
this.errs[lng] = errs;
}catch(err){
lng = this.defLang;
}
}
return this.errs[lng][code] || 'errorCode not found';
}
}
module.exports = new errorManager();
@@ -0,0 +1,13 @@
module.exports = {
// client error
CE0000: '請輸入參數',
CE0001: '請輸入帳號',
CE0002: '請輸入密碼',
CE0003: '使用者帳號或密碼錯誤',
// server error
SE0000: '資料查詢失敗',
SE0001: '資料新增失敗',
SE0002: '資料更新失敗',
SE0003: '資料刪除失敗'
}