Skip to content

logError swallows error details (such as cause) #6462

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cdauth opened this issue Apr 17, 2025 · 1 comment · May be fixed by #6464
Open

logError swallows error details (such as cause) #6462

cdauth opened this issue Apr 17, 2025 · 1 comment · May be fixed by #6464
Labels

Comments

@cdauth
Copy link
Contributor

cdauth commented Apr 17, 2025

Express configures finalhandler’s onError function in application.js:156 to call logError, which basically calls console.error(err.stack || err.toString()).

Some years ago logging err.stack was the only way to print out the error message and stack trace at once, as logging just err would miss some things (I don’t remember what). This has changed and nowadays it seems to be the other way round.

For example, there is now the Error.cause property allowing to specify a nested error. console.log(new Error("outer", { cause: new Error("inner") })) prints the nested error, while console.log(new Error("outer", { cause: new Error("inner") }).stack) misses it.

Async stack traces might be another example, although I couldn’t find a good way to test this.

In my concrete case, I am using Sequelize and an error occurred. Express basically logged the error as Error: with a stack trace that was not very helpful. After some investigation, I found out that logging the error object itself rather than its stack property reveals tons of information about the error that were otherwise missing. It seems that Sequelize errors contain nested errors in the parent and original properties, and those are logged as well when using console.error(error) but not with console.error(error.stack).

As a workaround, I am using this as the final middleware in my app:

	app.use((err, req, res, next) => {
		finalhandler(req, res, {
			env: app.get("env"),
			onerror: (err) => { console.error(err); }
		})(err);
	});
@cdauth cdauth added the bug label Apr 17, 2025
@Nitin-Mohapatra
Copy link

I’m working on this. Will open a PR soon 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants