webio-node/route/ResTool.js

49 lines
1.3 KiB
JavaScript

const errList = require('../includes/errorManager');
function send(req, res) {
if ('db' in res && typeof res.db == 'object' && 'close' in res.db && typeof res.db.close == 'function') res.db.close();
if ('db' in res && typeof res.db == 'object' && 'release' in res.db && typeof res.db.release == 'function') res.db.release();
let lngs = req.headers['accept-language'] || '';
lngs = lngs.split(',');
let lng = null;
if (lngs.length > 0) {
lng = lngs[0].substring(0, 2);
}
let json = {};
if (!('api_res' in res)) {
json = {
errorCode: 'ERR9999',
message: 'api not found',
status: 0
};
} else if (typeof res.api_res != 'object') {
json = {
errorCode: res.api_res,
message: errList(res.api_res, lng),
status: 0
};
} else {
json = {
data: res.api_res,
status: 1
};
}
if('sys_err' in res) {
json.sys_err = res.sys_err
}
res.send(json);
}
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 };