...
|
...
|
@@ -11,6 +11,7 @@ const config = global.yoho.config; |
|
|
const hostname = require('os').hostname();
|
|
|
const routeEncode = require('./route-encode');
|
|
|
const _ = require('lodash');
|
|
|
const limiterIpTime = 3600;
|
|
|
|
|
|
const forceNoCache = (res) => {
|
|
|
if (res) {
|
...
|
...
|
@@ -50,7 +51,10 @@ exports.notFound = () => { |
|
|
* @return {[type]}
|
|
|
*/
|
|
|
exports.serverError = () => {
|
|
|
return (err, req, res, next) => {
|
|
|
return async (err, req, res, next) => {
|
|
|
if (req.isCapturedError) {
|
|
|
return;
|
|
|
}
|
|
|
forceNoCache(res);
|
|
|
|
|
|
const uid = req.user ? req.user.uid : 0;
|
...
|
...
|
@@ -62,7 +66,7 @@ exports.serverError = () => { |
|
|
|
|
|
err.code = err.code || err.statusCode || 500;
|
|
|
|
|
|
if (req.isApmReport && !err.apiError && err.code !== 401) {
|
|
|
if (req.isApmReport && !err.apiError) { // apierror在node lib中已经上报过了 不需要再次上报
|
|
|
// 上报服务端错误
|
|
|
sender.addMessage({
|
|
|
measurement: 'error-report',
|
...
|
...
|
@@ -96,28 +100,32 @@ exports.serverError = () => { |
|
|
}
|
|
|
}
|
|
|
if (err.code === 9999991 || err.code === 9999992) {
|
|
|
let limitAPI = helpers.urlFormat('/3party/check', {refer: req.get('Referer') || ''});
|
|
|
let limitPage = helpers.urlFormat('/3party/check', {
|
|
|
refer: req.protocol + '://' + req.get('host') + req.originalUrl
|
|
|
});
|
|
|
let remoteIp = req.yoho.clientIp;
|
|
|
|
|
|
if (remoteIp.indexOf(',') > 0) {
|
|
|
let arr = remoteIp.split(',');
|
|
|
const isHuman = await cache.getAsync(`${config.app}:limiter:ishuman:${remoteIp}`);
|
|
|
|
|
|
remoteIp = arr[0];
|
|
|
}
|
|
|
if (!isHuman) {
|
|
|
if (remoteIp.indexOf(',') > 0) {
|
|
|
let arr = remoteIp.split(',');
|
|
|
|
|
|
cache.setAsync(`${config.app}:limiter:${remoteIp}`);
|
|
|
remoteIp = arr[0];
|
|
|
}
|
|
|
cache.setAsync(`${config.app}:limiter:${remoteIp}`, 1, limiterIpTime);
|
|
|
|
|
|
if (req.xhr) {
|
|
|
return res.status(510).json({
|
|
|
code: err.code,
|
|
|
data: {refer: limitAPI}
|
|
|
let limitAPI = helpers.urlFormat('/3party/check', {refer: req.get('Referer') || ''});
|
|
|
let limitPage = helpers.urlFormat('/3party/check', {
|
|
|
refer: req.protocol + '://' + req.get('host') + req.originalUrl
|
|
|
});
|
|
|
}
|
|
|
|
|
|
return res.redirect(limitPage);
|
|
|
if (req.xhr) {
|
|
|
return res.status(510).json({
|
|
|
code: err.code,
|
|
|
data: {refer: limitAPI}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
return res.redirect(limitPage);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!res.headersSent) {
|
...
|
...
|
|