glebdmitriew / node-unzip-2

node.js cross-platform unzip using streams
MIT License
44 stars 39 forks source link

Can't handle error while readstream get error #20

Open skyfore opened 6 years ago

skyfore commented 6 years ago
'use strict';

const fs = require('fs');
const unZip = require('unzip2');

const fswrap = filepath => new Promise((resolve, reject) => {
  const readable = fs.createReadStream(filepath).pipe(unZip.Extract({ path: './' }));
  readable.on('close', () => resolve(buf));
  readable.on('error', err => reject(err));
});

const main = async () => {
  await fswrap('./notexistfile');
};

main().catch(e => console.error(e));

In this case, ./notexistfile is not exist, and I can't catch error. What should I do if I want to catch error.

skyfore commented 6 years ago

I solve it with split code, such as

const fswrap = filepath => new Promise((resolve, reject) => {
  const readable = fs.createReadStream(filepath);
  const pipistream = readable.pip(unZip.Extract({ path: './' }));
  pipistream.on('close', () => resolve());
  readable.on('error', e => reject(e));
});

I do not know if it will parameter more problem, but I test can catch error or get right value.