add mysql error send to frontend

This commit is contained in:
Jay 2017-03-31 17:14:42 +08:00
parent c1b443b9d2
commit e529794956
12 changed files with 96 additions and 71 deletions

View File

@ -32,7 +32,18 @@ function send(req, res) {
};
}
if('sys_err' in res) {
json.sys_err = res.sys_err
}
res.send(json);
}
module.exports = { send: send };
const err = (res, err, n, code = null) => {
if(process.env.NODE_ENV != 'production') res.sys_err = 'toString' in err && typeof err.toString == 'function' ? err.toString() : err;
if(!code) return n();
return n(code);
}
module.exports = { send, err };

View File

@ -125,7 +125,7 @@ router
}
return n();
})
.catch(e => n('ERR8000'));
.catch(e => rt.err(res,e, n,'ERR8000'));
})
.post('/setdioinfo', (req,res,n) => {
if(!config.permission.dio) return n('ERR9000');
@ -177,7 +177,7 @@ router
Promise.all([pdi, pdo])
.then(r => n())
.catch(e => n('ERR8002'))
.catch(e => rt.err(res,e,n,'ERR8002'))
})
.all('*', rt.send);

View File

@ -46,11 +46,15 @@ router.use((err, req, res, n) => {
if (lngs.length > 0) {
lng = lngs[0].substring(0, 2);
}
res.send({
let json = {
errorCode: (typeof err != 'string' ? err.toString() : err),
message: errMng(err, lng),
status: 0
});
};
if('sys_err' in res && process.env.NODE_ENV != 'production') json.sys_err = res.sys_err;
res.send(json);
})
module.exports = router;

View File

@ -37,7 +37,7 @@ router
}
res.db.query(query + order, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
let dos = [];
let les = [];
@ -88,7 +88,7 @@ router
return n();
})
.catch(err => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
});
});
})
@ -104,7 +104,7 @@ router
let param = [config.db.db1, 'iogroup', arr.data.name, arr.data.devs];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8001');
if (err) return rt.err(res,err,n,'ERR8001');
let data = {};
data.record = [];
@ -129,7 +129,7 @@ router
`iogroupuid` = ?";
let param = [config.db.db1, 'iogroup', arr.data.name, arr.data.devs, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
if (err) return rt.err(res,err,n,'ERR8002');
let data = {};
data.record = [];
@ -147,7 +147,7 @@ router
let query = "delete from ??.?? where `iogroupuid` = ? ";
let param = [config.db.db1, 'iogroup', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
if (err) return rt.err(res,err,n,'ERR8003');
let data = {};
data.record = [];

View File

@ -28,7 +28,7 @@ router
let query = "select * from ??.??";
let param = [config.db.db1, 'ipcam'];
res.db.query(query, param, (err, row) => {
if(err) return n('ERR8000');
if(err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row)
@ -50,13 +50,13 @@ router
let query = "select count(*) as c from ??.?? where `ip` = ?";
let param = [config.db.db1, 'ipcam', arr.data.ip];
res.db.query(query, param, (err, row) => {
if(err || row.length == 0) return n('ERR8000');
if(err || row.length == 0) return rt.err(res,err,n,'ERR8000');
if(row[0]['c'] > 0) return n('ERR0027');
let query = "insert into ??.?? (`name`, `ip`) values (?,?)";
let param = [config.db.db1, 'ipcam', arr.data.name, arr.data.ip];
res.db.query(query, param, (err, row) => {
if(err) return n('ERR8001');
if(err) return rt.err(res,err,n,'ERR8001');
res.api_res = {
record: []
};
@ -74,7 +74,7 @@ router
let query = "delete from ??.?? where `uid` = ?";
let param = [config.db.db1, 'ipcam', arr.data.id];
res.db.query(query, param, (err, row) => {
if(err) return n('ERR8003');
if(err) return rt.err(res,err,n,'ERR8003');
res.api_res = {
record: []
};
@ -92,7 +92,7 @@ router
let query = "update ??.?? set `name` = ? where `uid` = ?";
let param = [config.db.db1, 'ipcam', arr.data.name, arr.data.id];
res.db.query(query, param, (err,row) => {
if(err) return n('ERR8002');
if(err) return rt.err(res,err,n,'ERR8002');
res.api_res = {
record: []
}

View File

@ -51,7 +51,7 @@ router
let query = "select `leonename`, `leoneip`, `leonelistuid` from ??.?? where `temporary` = '1' ";
let param = [config.db.db1, 'leonelist'];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
let data = {};
data.record = tool.checkArray(row);
@ -83,7 +83,7 @@ router
}
let order = " order by `leonelistuid` desc ";
res.db.query(query + order, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
tool.getLeoneRT(rts => {
let data = {};
@ -107,19 +107,19 @@ router
let query = "select count(*) as num from ??.?? where `temporary` = '0' ";
let param = [config.db.db1, 'leonelist'];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
if (row[0].num >= config.leone_limit) return n('ERR0056');
let query = "select count(*) from ??.?? where `leoneip` = ? ";
let p = [...param, arr.data.ip];
res.db.query(query, p, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
if (row[0].num > 0) return n('ERR0027');
let query = "insert into ??.?? (`leoneip`,`leonename`,`leonepassword`,`leone_add_date`,`leone_modify_date`) values (?,?,?,unix_timestamp(),unix_timestamp())";
let p = [...param, arr.data.ip, arr.data.name, arr.data.password];
res.db.query(query, p, (err, row) => {
if (err) return n('ERR8001');
if (err) return rt.err(res,err,n,'ERR8001');
let data = {};
data.record = [];
@ -139,7 +139,7 @@ router
let query = "delete from ??.?? where `leonelistuid` = ? ";
let param = [config.db.db1, 'leonelist', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
if (err) return rt.err(res,err,n,'ERR8003');
let data = {};
data.record = [];
res.api_res = data;
@ -163,7 +163,7 @@ router
`leonelistuid` = ? ";
let param = [config.db.db1, 'leonelist', arr.data.name, arr.data.password, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
if (err) return rt.err(res,err,n,'ERR8002');
let data = {};
data.record = [];
@ -172,7 +172,7 @@ router
let query = "select * from ??.?? where `leonelistuid` = ? ";
let param = [config.db.db1, 'leonelist', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n();
if (err) return rt.err(res,err,n);
if (row.length == 0) return n();
let { leoneip, leonename, leonepassword } = row[0];
@ -207,12 +207,12 @@ router
let query = "select count(*) as num from ??.?? where `temporary` = '0' or `leonelistuid` in (?)";
let param = [config.db.db1, 'leonelist', ids];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
if (row[0].num >= config.leone_limit) return n('ERR0056');
let query = "update ??.?? set `temporary` = '0', `leone_modify_date` = unix_timestamp() where `leonelistuid` in (?)";
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
if (err) return rt.err(res,err,n,'ERR8002');
let data = {};
data.record = [];
res.api_res = data;

View File

@ -36,7 +36,7 @@ router
let param = [config.db.db8, 'jcioclnt', config.db.db8, 'jcioclnt', config.db.db8, 'jcioclnt'];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row)
@ -63,7 +63,7 @@ router
let param = [config.db.db8, 'getLink', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
let ids = row[0].ids;
let id = ids.split(',');
@ -104,7 +104,7 @@ router
}
return n();
})
.catch(err => n('ERR8000'));
.catch(err => rt.err(res,err,n,'ERR8000'));
});
})
.post('/addlink', (req, res, n) => {
@ -173,7 +173,7 @@ router
let param = [config.db.db8, 'jcioclnt', (arr.data.active == 1 ? 1 : 0), arr.data.action, id.substr(2)];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
if (err) return rt.err(res,err,n,'ERR8002');
res.api_res = {
record: []
@ -197,7 +197,7 @@ router
let param = [config.db.db8, 'jcioclnt', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
if (err) return rt.err(res,err,n,'ERR8002');
res.api_res = {
record: []
@ -218,7 +218,7 @@ router
let param = [config.db.db8, 'getLink', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
let ids = row[0].ids;
let id = ids.split(',');
id.splice(0, 1);
@ -227,6 +227,7 @@ router
if (t != '') return t;
});
res.db.query(`use ${config.db.db8}`);
let query = "delete c,n from ??.?? n \
left join ??.?? c \
on \
@ -242,7 +243,7 @@ router
n.`jcioclntuid` in (?)";
let param = [config.db.db8, 'jcioclnt', config.db.db7, 'jcioclct', ida];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
if (err) return rt.err(res,err,n,'ERR8003');
res.api_res = {
record: []

View File

@ -27,13 +27,13 @@ router
let query = "select count(*) as num from ??.??";
let param = [config.db.db2, 'jciocert'];
res.db.query(query, param, (err, row) => {
if(err || row.length == 0) return n('ERR0023');
if(err || row.length == 0) return rt.err(res,err,n,'ERR0023');
let page = mysql.recordPage(row[0].num, p, per);
let query = "select * from ??.?? order by `ioeventtst` desc ";
let limit = ` limit ${page.rec_start} , ${per} `;
res.db.query(query + limit, param, (err, row) => {
if(err) return n('ERR8000');
if(err) return rt.err(res,err,n,'ERR8000');
let data = {};
data.page = page;
data.record = tool.checkArray(row);

View File

@ -27,7 +27,7 @@ router
let param = [config.db.db5, 'device'];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row)
@ -47,7 +47,7 @@ router
let param = [config.db.db5, 'device', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row),
@ -80,7 +80,7 @@ router
}
return n();
})
.catch(err => n('ERR8000'));
.catch(err => rt.err(res,err,n,'ERR8000'));
});
})
.post('/getporttype', (req, res, n) => {
@ -90,7 +90,7 @@ router
let param = [config.db.db5, 'porttype'];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row)
};
@ -115,7 +115,7 @@ router
let param = [config.db.db5, 'device', arr.data.name, arr.data.node, type, u, u];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8001');
if (err) return rt.err(res,err,n,'ERR8001');
res.api_res = {
record: []
@ -138,7 +138,7 @@ router
let query = "select count(*) as num from ??.?? where `node` = ? and `uid` != ?";
let param = [config.db.db5, 'device', arr.data.node, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
if (row[0].num > 0) return n('ERR0047');
let u = '';
@ -155,7 +155,7 @@ router
`uid` = ?";
let param = [config.db.db5, 'device', arr.data.name, arr.data.node, type, u, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
if (err) return rt.err(res,err,n,'ERR8002');
let q = "delete from ??.?? where `node` in (?)";
let p = [config.db.db6, 'jcmbrt', [arr.data.node, arr.data.original_node]];
@ -187,7 +187,7 @@ router
d.`uid` = ?";
let param = [config.db.db5, 'device', config.db.db5, 'iolist', config.db.db5, 'aioset', config.db.db6, 'jcmbrt', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
if (err) return rt.err(res,err,n,'ERR8003');
res.api_res = {
record: []
};
@ -218,7 +218,7 @@ router
let param = [config.db.db6, 'jcmbrt', config.db.db5, 'device', config.db.db5, 'iolist', arr.data.type, config.db.db5, 'aioset', arr.data.id, arr.data.type];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row)
@ -235,7 +235,7 @@ router
let query = "select * from ??.?? where `uid` = ? ";
let param = [config.db.db5, 'iolist', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row)
@ -263,14 +263,14 @@ router
and `addr` = ?";
let param = [config.db.db5, 'iolist', arr.data.id, arr.data.type, arr.data.addr];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
if (row[0].c > 0) return n('ERR0054');
let query = "insert into ??.?? (`devuid`,`type`,`addr`,`num`,`datalen`,`ctime`,`mtime`) values (?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp())";
let param = [config.db.db5, 'iolist', arr.data.id, arr.data.type, arr.data.addr, arr.data.num, dlen];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8001');
if (err) return rt.err(res,err,n,'ERR8001');
res.api_res = {
record: []
@ -301,7 +301,7 @@ router
and i2.`uid` is not null ";
let param = [config.db.db5, 'iolist', config.db.db5, 'iolist', arr.data.addr, arr.data.id, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
if (row[0].c > 0) return n('ERR0054');
let q = "update ??.?? set \
@ -313,7 +313,7 @@ router
`uid` = ?";
let p = [config.db.db5, 'iolist', arr.data.addr, arr.data.num, dlen, arr.data.id];
res.db.query(q, p, (err, row) => {
if (err) return n('ERR8002');
if (err) return rt.err(res,err,n,'ERR8002');
res.api_res = {
record: []
@ -338,7 +338,7 @@ router
i.`uid` = ?";
let param = [config.db.db5, 'iolist', config.db.db5, 'aioset', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
if (err) return rt.err(res,err,n,'ERR8003');
res.api_res = {
record: []
@ -359,7 +359,7 @@ router
i.`uid` = ?";
let param = [config.db.db5, 'aioset', config.db.db5, 'iolist', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row)
@ -376,7 +376,7 @@ router
let query = "select * from ??.?? where `uid` = ?";
let param = [config.db.db5, 'aioset', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row)
@ -394,7 +394,7 @@ router
let query = "delete from ??.?? where `uid` = ?";
let param = [config.db.db5, 'aioset', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
if (err) return rt.err(res,err,n,'ERR8003');
res.api_res = {
record: []
};
@ -418,7 +418,7 @@ router
let query = "select count(*) as count from ??.?? where `iouid` = ? and `portnum` = ?";
let param = [config.db.db5, 'aioset', arr.data.iouid, arr.data.portnum];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
if (row[0].count > 0) return n('ERR0054');
@ -426,7 +426,7 @@ router
(?, ?, ?, ?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp())";
let p = [config.db.db5, 'aioset', arr.data.iouid, arr.data.name, arr.data.portnum, arr.data.type, arr.data.range_min, arr.data.range_max, arr.data.scale_min, arr.data.scale_max];
res.db.query(q, p, (err, row) => {
if (err) return n('ERR8001');
if (err) return rt.err(res,err,n,'ERR8001');
res.api_res = {
record: []
@ -458,7 +458,7 @@ router
and `uid` != ?";
let param = [config.db.db5, 'aioset', arr.data.iouid, arr.data.portnum, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (err || row.length == 0) return rt.err(res,err,n,'ERR8000');
if (row[0].length > 0) return n('ERR0054');
let query = "update ??.?? set \
@ -473,7 +473,7 @@ router
where `uid` = ?";
let param = [config.db.db5, 'aioset', arr.data.name, arr.data.portnum, arr.data.type, arr.data.range_min, arr.data.range_max, arr.data.scale_min, arr.data.scale_max, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
if (err) return rt.err(res,err,n,'ERR8002');
res.api_res = {
record: []
@ -532,7 +532,7 @@ router
let param = [config.db.db10, 'jcmbrt', config.db.db5, 'device', config.db.db5, 'iolist', config.db.db5, 'aioset', arr.data.ids, arr.data.stime, arr.data.etime];
res.db.query(query, param, (err,row) => {
if(err) return n('ERR8000');
if(err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row)
}

View File

@ -41,7 +41,7 @@ router
}
res.db.query(query + order, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row),
@ -106,7 +106,7 @@ router
return n();
})
.catch(err => {
return n();
return rt.err(res,err,n);
})
});
})
@ -121,7 +121,7 @@ router
let param = [config.db.db1, 'ioschedulet', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
if (err) return rt.err(res,err,n,'ERR8003');
res.api_res = {
record: []
@ -142,7 +142,7 @@ router
let param = [config.db.db1, 'ioschedulet', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
if (err) return rt.err(res,err,n,'ERR8002');
res.api_res = {
record: []
@ -198,7 +198,7 @@ router
let param = [config.db.db1, 'ioschedulet', arr.data.name, active, arr.data.devs, arr.data.action, arr.data.min, arr.data.hour, d, m, w];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8001');
if (err) return rt.err(res,err,n,'ERR8001');
res.api_res = {
record: []
@ -257,7 +257,7 @@ router
let param = [config.db.db1, 'ioschedulet', arr.data.name, active, arr.data.devs, arr.data.action, arr.data.min, arr.data.hour, d, m, w, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
if (err) return rt.err(res,err,n,'ERR8002');
res.api_res = {
record: []

View File

@ -112,7 +112,7 @@ router
let query = "select * from ??.?? where `account` = ? and `user_password` = ?";
res.db.query(query, [config.db.db1, 'userlist', arr.data.account, arr.data.password], (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res, err, n, 'ERR8000');
if (row.length == 0) return n('ERR0019');
delete row[0]['user_password'];
@ -170,7 +170,7 @@ router
}
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
if (err) return rt.err(res, err, n, 'ERR8000');
for (var i in row) {
delete row[i]['user_password'];
@ -192,7 +192,7 @@ router
let query = "delete from ??.?? where `account` = ?";
let param = [config.db.db1, 'userlist', arr.data.account];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR0020');
if (err) return rt.err(res,err, n, 'ERR0020');
let data = {};
data.record = [];
@ -222,7 +222,7 @@ router
param.push(arr.data.account);
res.db.query(query, param, (err, row) => {
if (err) return n('ERR0021');
if (err) return rt.err(res,err,n,'ERR0021');
let data = {};
data.record = [];
res.api_res = data;
@ -243,7 +243,7 @@ router
let param = [config.db.db1, 'userlist', arr.data.account, arr.data.password, w.toString(), r.toString()];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR0022');
if (err) return rt.err(res,err,n,'ERR0022');
let data = {};
data.record = [];
@ -354,7 +354,7 @@ router
return n();
})
.catch(e => {
return n();
return rt.err(res, e, n);
});
})
.post('/getselectlist', (req, res, n) => {
@ -450,7 +450,16 @@ router
})
})
.post('/gettimezoneset', (req,res,n) => {
let query = "select * from ??.?? where `type` = ? limit 1";
let param = [config.db.db1, 'system', 'timezone'];
res.db.query(query, param, (err, row) => {
if(err) return rt.err(res,err,n, 'ERR8000');
res.api_res = {
record: tool.checkArray(row)
}
return n();
});
})
.all('*', rt.send);

View File

@ -167,7 +167,7 @@ router
let param = [config.db.db9, 'wristband', config.db.db9, 'rawdata', config.db.db9, 'lastdata', config.db.db9, 'location', config.db.db9, 'location',];
res.db.query(query, param, (err,row) => {
if(err) return n('ERR8000');
if(err) return rt.err(res,err,n,'ERR8000');
res.api_res = {
record: tool.checkArray(row)