You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Express configures finalhandler’s
onError
function in application.js:156 to call logError, which basically callsconsole.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 justerr
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, whileconsole.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 itsstack
property reveals tons of information about the error that were otherwise missing. It seems that Sequelize errors contain nested errors in theparent
andoriginal
properties, and those are logged as well when usingconsole.error(error)
but not withconsole.error(error.stack)
.As a workaround, I am using this as the final middleware in my app:
The text was updated successfully, but these errors were encountered: