Skip to content

[Bug]: TypeError encountered when logging class extending superclass which contains protected property #2103

@turchiad

Description

@turchiad

🔎 Search Terms

TypeError

The problem

Regression in winston v3.7.1 caused the code shown further within to produce a TypeError.

See below error log:

      Object.assign(msg, this.defaultMeta, msgClone);
             ^

TypeError: Cannot assign to read only property 'canBeAnything' of object 'Error: This must not be empty'
    at Function.assign (<anonymous>)
    at DerivedLogger._addDefaultMeta (<PATH>/node_modules/winston/lib/winston/logger.js:639:14)
    at DerivedLogger.<computed> [as info] (<PATH>/node_modules/winston/lib/winston/create-logger.js:80:14)
    at Object.<anonymous> (<PATH>/index.js:31:5)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

What version of Winston presents the issue?

v3.7.1

What version of Node are you using?

v.17.8.0

If this worked in a previous version of Winston, which was it?

v3.6.0

Minimum Working Example

const {createLogger, format, transports} = require('winston');
const {json} = format;

const log = createLogger({
	level: 'info',
	defaultMeta: {service: 'database-service'},
	transports: [
		new transports.Console({
			format: json(),
			level: 'info',
		}),
	]
});

class SuperError extends Error {
	constructor() {
		super();
		Object.defineProperty(this, 'canBeAnything', { enumerable: true, value: ''});
	}
}

class ThisError extends SuperError {
	message;

	constructor() {
		super();
		this.message = "This must not be empty";
	}
}

log.info(new ThisError());

Additional information

This was encountered initially when using winston to report an error produced by mssql. mssql has their own error classes and when winston upticked from v3.6.0 to v3.7.1 it broke our error reporting.

Original code is closed source, but I have basically boiled down the minimum required to reproduce this error above.

Worth noting, perhaps, but extend Error is extra and I don't believe it's needed. Additionally, this TypeError is not reproduced if this.message is omitted or is equal to ''.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions