Skip to content

Commit

Permalink
chore(logger): unhandledRejection run with domain
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyoukun committed May 29, 2018
1 parent 04595fd commit f17842f
Showing 1 changed file with 22 additions and 63 deletions.
85 changes: 22 additions & 63 deletions bin/proxy/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const isDeaded = false;

// 阻止进程因异常而退出
process.on('uncaughtException', function(e) {

// Mac和linux权限不足时给予提醒
if (/\blisten EACCES\b/.test(e.message) && config.httpPort < 1024 && (serverOS.isOSX || serverOS.isLinux)) {
logger.error('This is OSX/Linux, you may need to use "sudo" prefix to start server.\n');
}
Expand All @@ -33,83 +35,40 @@ process.on('uncaughtException', function(e) {
process.on('warning', function(warning) {
const key = String(warning);
const errStr = warning && warning.stack || String(warning);
const Content = `<p><strong>错误堆栈</strong></p><p><pre><code>${errStr}</code></pre></p>`;

logger.error(errStr);

const Content = [
'<p><strong>错误堆栈</strong></p>',
'<p><pre><code>',
errStr,
'</code></pre></p>',
].join('');


setImmediate(function() {
require('util/mail/mail.js').SendMail(key, 'js', 600, {
'Title': key,
'runtimeType': 'warning',
'Content': Content
});
});

});


process.on('unhandledRejection', (reason = {}, p = {}) => {
let errStr = String(reason.stack),
mod_act,
module,
REQUEST;

const key = String(reason.message);

if (p && p.domain && p.domain.currentContext) {
mod_act = p.domain.currentContext.mod_act;
module = p.domain.currentContext.module;
REQUEST = p.domain.currentContext.window.request.REQUEST;
}

if (errStr === 'undefined') {

const reasonStr = JSON.stringify(reason);

logger.error('unhandledRejection reason: ' + reasonStr);

if (reasonStr === '{}') {
return;
}

errStr = reasonStr;
}

logger.error(errStr);

let Content = [
'<p><strong>错误堆栈</strong></p>',
'<p><pre><code>',
errStr,
'</code></pre></p>',
].join('');

if (mod_act) {
Content += `<p><strong>mod_act: </strong>${mod_act}</p>`;
}
if (module) {
Content += `<p><strong>module: </strong>${module}</p>`;
}
if (REQUEST) {
Content += `<p><strong>url: </strong>${REQUEST.protocol}://${REQUEST.hostname}${REQUEST.href}</p>`;
}

if (serverOS.isWindows) {
// return;
process.on('unhandledRejection', (errorOrReason, currPromise) => {
const errStr = String(errorOrReason && errorOrReason.stack || JSON.stringify(errorOrReason));
const key = String(errorOrReason && errorOrReason.message);
const Content = `<p><strong>错误堆栈</strong></p><p><pre><code>${errStr}</code></pre></p>`;

// 恢复上下文
if (currPromise && currPromise.domain) {
currPromise.domain.run(function() {
logger.error(`unhandledRejection reason: ${errStr}`);
setImmediate(function() {
require('util/mail/mail.js').SendMail(key, 'js', 600, {
'Title': key,
'runtimeType': 'warning',
'Content': Content
});
});
});
} else {
logger.error(`unhandledRejection reason: ${errStr}`);
}

require('util/mail/mail.js').SendMail(key, 'js', 600, {
'Title': key,
'runtimeType': 'unhandledRejection',
'Content': Content
});
});

process.noProcessWarnings = true;
Expand Down

0 comments on commit f17842f

Please sign in to comment.