S
S
Svyatoslav Khusamov2018-07-11 12:22:43
Node.js
Svyatoslav Khusamov, 2018-07-11 12:22:43

How to make NodeJS output file name and line number on error?

5b45cc465b694552529476.jpeg
Here is a simple script. Which clearly demonstrates the problem with readFile. If we read a non-existent file, then an error will be generated. But in which file the error occurred and on which line - We will not know.
In such a simple script, it is not difficult to find an error. But if readFile is hidden it will be deep. In some third party module? Or even worse in several times nested module? Then the error will be harder to find.
How to make a node output file name and line number?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Svyatoslav Khusamov, 2018-07-11
@khusamov

// Предварительная версия обработки ошибок функции Fs.readFile().
    let configFileData;
    try {
      configFileData = await readFile(configFilePath, {encoding: 'utf8'});
    } catch(e) {
      Error.captureStackTrace(e);
      throw e;
    }

L
Lynn "Coffee Man", 2018-07-11
@Lynn

No way.
This information is simply not in the error returned by fs.readFile .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question