Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript errors not showing up in console?

fields is undefined in the following code snipped, but it is not logged to the console when the error happens. In this specific instance, why, and what is the de facto way to handle this?

"Testing" is logged to the console (Line #2), but the undefined variable fields (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) {
  console.log("Testing")
  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.

like image 693
Connorelsea Avatar asked Sep 12 '16 01:09

Connorelsea


People also ask

How do I show JavaScript errors?

Right-click anywhere in the webpage and then select Inspect. Or, press F12 . DevTools opens next to the webpage. In the top right of DevTools, the Open Console to view errors button displays an error about the webpage.

How do I show hidden console errors?

Even after checking the 'Errors' under 'Default', I was not able to see the errors in the console. I then navigated to settings (or press F1 ) and on the bottom of the page, you will see 'Restore defaults and reload' option. After restoring to defaults, I am able to see the console errors. Save this answer.

How do I fix JavaScript console error?

Go to the screen where you are experiencing the error. In Chrome, navigate to View > Developer > JavaScript Console or More Tools > JavaScript Console or press Ctrl + Shift + J. The error console will open. If you don't see any errors try reloading the page.

How do you show warnings in console?

warn():- This method is used to log a warning message to the console.


2 Answers

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.

like image 91
Jacob Avatar answered Oct 14 '22 09:10

Jacob


Because you didn't actually log the error?

export function post(req, res) {
  console.log("Testing")
  User.create( getFields(req, ["name_first", "name_last"]) )
    .then(user =>  respondJSON (res, fields, { status: 201 }))
    .catch(err => {
      console.error(err);
      respondError(res, err);
    });
}
like image 40
Jason Livesay Avatar answered Oct 14 '22 10:10

Jason Livesay