Skip to content

logError swallows error details (such as cause) #6462

@cdauth

Description

@cdauth

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);
	});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions