Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Error.cause not logged correctly #2446

Open
lexctk opened this issue Apr 9, 2024 · 1 comment 路 May be fixed by #2447
Open

[Bug]: Error.cause not logged correctly #2446

lexctk opened this issue Apr 9, 2024 · 1 comment 路 May be fixed by #2447

Comments

@lexctk
Copy link

lexctk commented Apr 9, 2024

馃攷 Search Terms

Error.cause, cause property

The problem

Error.cause is not logged correctly.
For reference. Error.cause was added to es2022.

This seems to have been fixed in logform @#winstonjs/logform#226, but not in winston.

What version of Winston presents the issue?

v3.13.0

What version of Node are you using?

v21.6.2

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

No response

Minimum Working Example

const log = winston.createLogger({
	format: format.combine(
		format.errors({ cause: true }),
		format.json(),
	),
	transports: [ new transports.Console() ],
});

log.error(new Error("I'm an error", { cause: new Error("with a cause") }));

// {"cause":{},"level":"error","message":"I'm an error"}

Additional information

No response

@lexctk
Copy link
Author

lexctk commented Apr 9, 2024

The current workaround is to pass cause as a string, which seems to work

log.error(new Error("I'm an error", { cause: "with a string cause" }));

// {"cause":"with a string cause","level":"error","message":"I'm an error"}

but defeats the purpose, eg, a more common use case:

try {
  connectToDatabase();
} catch (err) {
  throw new Error("Connecting to database failed.", { cause: err });
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause

@lexctk lexctk linked a pull request Apr 9, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant