10000 Merge pull request #1420 from log4js-node/1417-typeerror-cannot-conve… · log4js-node/log4js-node@4027fc7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4027fc7

Browse files
authored
Merge pull request #1420 from log4js-node/1417-typeerror-cannot-convert-object-to-primitive-value-in-cluster-mode
fix(LoggingEvent): serde for object with null prototype
2 parents f5cac41 + e1efb82 commit 4027fc7

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/LoggingEvent.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ class SerDe {
2222

2323
canSerialise(key) {
2424
if (typeof key === 'string') return false;
25-
return key in this.serMap;
25+
try {
26+
return key in this.serMap;
27+
} catch (e) {
28+
return false;
29+
}
2630
}
2731

2832
serialise(key) {

test/tap/LoggingEvent-test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ test('LoggingEvent', (batch) => {
2020
});
2121

2222
batch.test('should serialise to flatted', (t) => {
23+
const nullPrototype = Object.create(null);
24+
nullPrototype.hello = 'world';
2325
const event = new LoggingEvent(
2426
'cheese',
2527
levels.DEBUG,
@@ -33,6 +35,7 @@ test('LoggingEvent', (batch) => {
3335
'-Infinity',
3436
undefined,
3537
'undefined',
38+
nullPrototype,
3639
],
3740
{
3841
user: 'bob',
@@ -44,7 +47,7 @@ test('LoggingEvent', (batch) => {
4447
t.equal(rehydratedEvent.startTime, '2018-02-04T18:30:23.010Z');
4548
t.equal(rehydratedEvent.categoryName, 'cheese');
4649
t.equal(rehydratedEvent.level.levelStr, 'DEBUG');
47-
t.equal(rehydratedEvent.data.length, 9);
50+
t.equal(rehydratedEvent.data.length, 10);
4851
t.equal(rehydratedEvent.data[0], 'log message');
4952
t.equal(rehydratedEvent.data[1], '__LOG4JS_NaN__');
5053
t.equal(rehydratedEvent.data[2], 'NaN');
@@ -54,6 +57,11 @@ test('LoggingEvent', (batch) => {
5457
t.equal(rehydratedEvent.data[6], '-Infinity');
5558
t.equal(rehydratedEvent.data[7], '__LOG4JS_undefined__');
5659
t.equal(rehydratedEvent.data[8], 'undefined');
60+
t.equal(
61+
Object.entries(rehydratedEvent.data[9]).length,
62+
Object.entries(nullPrototype).length
63+
);
64+
t.equal(rehydratedEvent.data[9].hello, 'world');
5765
t.equal(rehydratedEvent.context.user, 'bob');
5866
t.end();
5967
});

0 commit comments

Comments
 (0)
0