chore: merged fix for stacktrace bug

This commit is contained in:
Gareth Jones 2018-03-01 07:50:40 +11:00
commit bea20cc0ce
2 changed files with 25 additions and 6 deletions

View File

@ -27,8 +27,8 @@ class LoggingEvent {
const logData = this.data.map((e) => {
// JSON.stringify(new Error('test')) returns {}, which is not really useful for us.
// The following allows us to serialize errors correctly.
if (e && e.stack && CircularJSON.stringify(e) === '{}') {
e = { message: e.message, stack: e.stack };
if (e && e.message && e.stack) {
e = Object.assign({ message: e.message, stack: e.stack }, e);
}
return e;
});
@ -41,9 +41,9 @@ class LoggingEvent {
try {
const rehydratedEvent = CircularJSON.parse(serialised);
rehydratedEvent.data = rehydratedEvent.data.map((e) => {
if (e && e.stack) {
const fakeError = new Error(e.message);
fakeError.stack = e.stack;
if (e && e.message && e.stack) {
const fakeError = new Error(e);
Object.keys(e).forEach((key) => { fakeError[key] = e[key]; });
e = fakeError;
}
return e;

View File

@ -36,17 +36,30 @@ if (cluster.isMaster) {
test('cluster master', (batch) => {
batch.test('events should be logged', (t) => {
t.equal(logEvents.length, 3);
t.equal(logEvents[0].categoryName, 'master');
t.equal(logEvents[0].pid, masterPid);
t.equal(logEvents[1].categoryName, 'worker');
t.equal(logEvents[1].pid, workerPid);
// serialising errors with stacks intact
t.type(logEvents[1].data[1], 'Error');
t.contains(logEvents[1].data[1].stack, 'Error: oh dear');
// serialising circular references in objects
t.type(logEvents[1].data[2], 'object');
t.type(logEvents[1].data[2].me, 'object');
// serialising errors with custom properties
t.type(logEvents[1].data[3], 'Error');
t.contains(logEvents[1].data[3].stack, 'Error: wtf');
t.equal(logEvents[1].data[3].alert, 'chartreuse');
// serialising things that are not errors, but look a bit like them
t.type(logEvents[1].data[4], 'object');
t.equal(logEvents[1].data[4].stack, 'this is not a stack trace');
t.equal(logEvents[2].categoryName, 'log4js');
t.equal(logEvents[2].level.toString(), 'ERROR');
t.equal(logEvents[2].data[0], 'Unable to parse log:');
t.end();
});
@ -63,9 +76,15 @@ if (cluster.isMaster) {
});
} else {
const workerLogger = log4js.getLogger('worker');
// test for serialising circular references
const circle = {};
circle.me = circle;
workerLogger.info('this is worker', new Error('oh dear'), circle);
// test for serialising errors with their own properties
const someError = new Error('wtf');
someError.alert = 'chartreuse';
// test for serialising things that look like errors but aren't.
const notAnError = { stack: 'this is not a stack trace' };
workerLogger.info('this is worker', new Error('oh dear'), circle, someError, notAnError);
// can't run the test in the worker, things get weird
process.send({
type: '::testing',