0

I have written a function that returns a promise to read a file line by line asynchronously but the problem is that if the file doesn't exist an error is thrown and the application crashes. I have surrounded the code in a try catch block but the code execution never goes into the catch block but terminates.

Secondly can I reject a promise and then return a json object from the promise?

Like reject({success: false});

const fs = require('fs');
const readline = require('readline');

class fileParser{
    readPolicyFile = function(filePath, fileName) {
        return new Promise((resolve, reject) => {
            try {
                let response = {};
                let readInterface = readline.createInterface({
                    input: fs.createReadStream(`${filePath}/${fileName}`),
                    terminal: false
                });
    
                readInterface
                    .on('line', line => {
                        // Do processing here to create the response
                    })
                    .on('close', () => {
                        resolve(response);
                    });
            } catch(err){
                console.log("Inside catach block");
                reject(err);
            }
        })
    }
}


module.exports = new fileParser();

Error

Faisal
  • 403
  • 4
  • 18
  • You need to handle the `error` event on that stream. Not wrap the code in `try`/`catch` – Bergi Jan 17 '21 at 11:55
  • Thanks @Bergi and @evolutionxbox, I was actually handling it in readInterface but it needs to be handled in the `fs` readstream. This did the trick for me. – Faisal Jan 17 '21 at 12:10

1 Answers1

2

You will never see Inside catach block line, because createReadStream will not throw an error as "normal". It will emit error event when it want to throw an error.

What you need is handle error of createReadStream (not of readInterface)

const fs = require('fs');
const readline = require('readline');

class fileParser {
  readPolicyFile = function (filePath, fileName) {
    return new Promise((resolve, reject) => {
      let response = {};

      // create a stream and handle it's error event
      const stream = fs.createReadStream(`${filePath}/${fileName}`);
      stream.on('error', (error) => {
        reject(error);
        // or reject({success: false}), but why call reject with data
        // If you don't want to handle error of readPolicyFile function, just call resolve({success: false})
      });

      let readInterface = readline.createInterface({
        input: stream,
        terminal: false
      });

      readInterface
        .on('line', line => {
          // Do processing here to create the response
        })
        .on('close', () => {
          resolve(response);
        });
    })
  }
}

module.exports = new fileParser();
hoangdv
  • 15,138
  • 4
  • 27
  • 48
  • Thanks @hoangdv that is a good point I don't need to reject the promise as I don't want to handle the error and just return error response. – Faisal Jan 17 '21 at 12:34