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

View File

@ -1,4 +1,5 @@
{
"port": 3000,
"db": {
"mysql": {
"user": "root",

View File

@ -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();

View File

@ -0,0 +1,13 @@
module.exports = {
// client error
CE0000: '請輸入參數',
CE0001: '請輸入帳號',
CE0002: '請輸入密碼',
CE0003: '使用者帳號或密碼錯誤',
// server error
SE0000: '資料查詢失敗',
SE0001: '資料新增失敗',
SE0002: '資料更新失敗',
SE0003: '資料刪除失敗'
}

View File

@ -6,6 +6,7 @@ const koaBody = require('koa-body');
const mysql = require('../../libs/mysql_pool.js');
const so = require('../../libs/storeObject');
const config = require('../../config.json');
const errorMng = require('./errorManager');
// routes
const user_api = require('./user.js');
@ -28,26 +29,39 @@ router.use(async(c, n) => {
}
})
}
// console.log(c.headers['accept-language'])
try {
await n();
} catch (e) {
console.log(e);
}
if ('db' in c && typeof c.db == 'object' && 'release' in c.db && typeof c.db.release == 'function') c.db.release();
switch (typeof c.body) {
case 'undefined':
c.body = { errorCode: 'ERR9999', status: 0 };
break;
case 'string':
c.body = { errorCode: c.body, status: 0 };
break;
default:
c.body = {
data: c.body,
status: 1
}
break;
} catch (e) {
let msg = errorMng.getMsg(e, c.headers['accept-language']);
c.body = {
errorCode: e,
message: msg,
servErr: c.serr ? c.serr.toString() : '',
status: 0
}
if(e[0] == 'C' || e[0] == 'c') c.status = 400;
if(e[0] == 'S' || e[0] == 's') c.status = 500;
}
if ('db' in c && typeof c.db == 'object' && 'release' in c.db && typeof c.db.release == 'function') c.db.release();
// switch (typeof c.body) {
// case 'undefined':
// c.body = { errorCode: 'ERR9999', status: 0 };
// break;
// case 'string':
// c.body = { errorCode: c.body, status: 0 };
// break;
// default:
// c.body = {
// data: c.body,
// status: 1
// }
// break;
// }
})

View File

@ -9,15 +9,15 @@ const mongo = require('../../libs/mongo_model.js');
router
.post('/login', async(c, n) => {
let arr = c.request.body;
if (!arr.data) throw 'C_EDATA';
if (!arr.data.account) throw 'C_EACCOUNT';
if (!arr.data.password) throw 'C_EPASSWORD';
if (!arr.data) throw 'CE0000';
if (!arr.data.account) throw 'CE0001';
if (!arr.data.password) throw 'CE0002';
try {
let user = await c.syncQuery('select `uid`,`account`,`password`,`name`,`email` from ??.?? where `account` = ?', ['lora', 'user', arr.data.account])
if (user.length == 0) throw 'user not exists';
if (user.length == 0) throw 'CE0003';
if (!crypto.comparePass(arr.data.password, user[0].password)) throw 'password not match';
if (!crypto.comparePass(arr.data.password, user[0].password)) throw 'CE0003';
delete user[0].password;
@ -25,7 +25,9 @@ router
record: user
}
} catch (err) {
throw 'DB Query Error';
if(typeof err == 'string') throw err;
c.serr = err;
throw 'SE0000';
}
let u = c.body.record[0];