Connorelsea Connorelsea - 1 year ago 97
Javascript Question

JavaScript errors not showing up in console?

is undefined in the following code snipped, but it is not logged to the console when the error happens. Why?

is logged to the console (Line #2), but the undefined variable
(Line #4) is not being reported. The error is returned in an API response (Line #5) but with no relevant information such as line #, stack trace, etc.

How can I make errors like this log to the console, and why are they not?

export function post(req, res) {
User.create( getFields(req, ["name_first", "name_last"]) )
.then(user => respondJSON (res, fields, { status: 201 }))
.catch(err => respondError (res, err))

Since the catch is responding with an error, I get the following API response:

"error": true,
"data": {
"message": "fields is not defined"

I am using Babel 6 and babel-node to run my code through NPM scripts. I am using morgan logging as well. Removing the middleware for logging does not alter the error output.

Answer Source

The automatic logging to console is a mechanism for unhandled exceptions. Because Promises automatically catch exceptions in the callbacks, the exceptions are no-longer unhandled, so nothing will be automatically logged.

If you want it to be logged, you could perhaps add a throw err at the end of your catch block. This will convert it into an unhandled promise rejection, which is typically handled similarly to an unhandled exception.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download