What is the purpose of console logging an error?


#1

Question

What is the purpose of console logging an error?

Answer

In this exercise we are asked to:

Log the error to the console if it exists, otherwise log the retrieved rows.

But as we may already be guessing or have a good idea of why. We log errors to the console not just because we need to know that there is an error, the error object that is sent on that response always contains important information that will help us figure out or it will at least point us to the direction where we might figure out the reason for that error, also thought as getting to the root of the problem.

in the exercise we have:

const db = require('./db');

db.all('SELECT * from NonexistentTable', (err, rows) => {
   ...
});

now if we filled it correctly to get that error message, if this was a real query to a database, once we open the console we would see something like so for example:

[SQLITE_ERROR] SQL error or missing database (no such table: NonexistentTable)

or we could get:

[SQLITE_ERROR] file is not a database

In a more general sense, errors when related to a file, they will also include some numbers, the first set is the line that originated the error or where the issue was caught, the second one is the likely character that might have caused the error, for example, if I had a file and in it I only had:

db.all('SELECT * from NonexistentTable', (err, rows) => {
   ...
});

it will raise an error like this one:

SyntaxError: Unexpected token: db.all(…) (18:3)
------------------------------------------^^^

There it means that it doesn’t know what db is, I had it on line 18 of my file and the third character is where it was caught, (.) and then I realize I didn’t declare db requiring SQLite.

As we can see, logging errors is a helpful practice in the development face of an application, and it is important to know that we can count on those errors to help us figure out some errors and bugs that we get on the way.