Treating error in callback function

Asked

Viewed 213 times

2

I’m trying to fix a mistake in the following code:

const fs = require('fs')

fs.readFile('dados/arqui2vo.json', (err, data) => {

    if (err) throw new Error(`Can't find the file`)

When I run the code, this message appears on the console:

c:\Users\<User>\Documents\Node\readFile.js:5
if (err) throw new Error(`Can't find the file`)
         ^

Error: Can't find the file
    at ReadFileContext.fs.readFile [as callback] (c:\Users\<User>\Documents\Node\readFile.js:5:20)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:237:13)

How can I show the error message I created? This is the message I’d like to appear on my console:

Error: Can't find the file
  • 1

    Welcome to Stack Overflow in English. Please click on edit and translate the question.

  • 1

    So far you have not edited your question. Our colleague warned you and there is no editing in it. -1

1 answer

2


The error shown in the terminal is being created on the following line:

if (err) throw new Error('Can\'t find the file');

Note that you are not treating the error in the most usual way since you are creating a new error (throw new Error), which will probably not be dealt with at another time, which will issue an error in the terminal, as you reported in your question.

So instead of creating a new error within the callback, seek only to display information on console, to let you know that an error has occurred. Something like this:

if (err) {
  console.error('Ocorreu um erro ao tentar ler o arquivo.');
}

That way, you’re performing the due process of error, and the Node.js process will continue to run normally, as the error has been properly handled.

And if you want to quit the application when an error occurs, just use the process.exit:

if (err) {
  console.error('<sua mensagem de erro aqui>');
  process.exit(1);
}

In the code above, we pass 1 as the first argument for the method process.exit, to indicate that an error caused the termination of the process.


Note that for larger applications that will be used in a production environment, display only a warning message by console may not be enough. Search in depth for how to treat a production error if you want to know more.

  • But I wanted you to stop the execution of the code at that time and show the type of error that occurred and/or do a check in the file, in case it is missing an item, also stop the execution and show that in the file there is no such information.

  • I edited my question showing a solution to stop the Node.js process.

  • Ok, thank you. You have know me indicate a study source to treat the errors in a way more suitable for production?

  • Depends on the application. I made a question on the subject recently. You can also use more advanced services (but often require a subscription), such as the Sentry for this... Initially, use a library as Winston or Pin might be a good idea.

  • I get it, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.