Uncaught exception when folder doesn't have permission
I have an issue when trying to build an archive when the folder on which to create the archive doesn't have write access. My code is as follow:
var output = fs.createWriteStream( zipFileName );
var archive = archiver('zip');
output.on( 'close', function() {
if ( typeof( callback ) === 'function' ) {
callback( null, { status: 'completed', bytes: archive.pointer()});
}
});
archive.on( 'error', function(err) {
if ( typeof( callback ) === 'function' ) {
callback( err );
}
});
archive.pipe( output );
archive.bulk([
{
expand: true,
cwd: folderName,
src: ["**/*"],
dot: true
}
]);
archive.finalize();
// note: calback is a function passed to this code and zipFileName contains the full path of the zip file to create.
So if the folder doesn't have write access, the .on( 'error' ... handler is not called but an exception is thrown latter on. Even if this whole code is put within a try/catch section, this exception is not caught.
Any idea, help?
Before opening the stream (fs.createWriteStream( zipFileName )) you may want to check write access by using fs.access.
Archive errors will be related to the archive, not to the stream you're piping to.
@soyuka thanks for chiming in on the last few issues posted. ive been a bit busier with personal life lately.
@david-rouchet i think you need to hook into the output file stream error event. not really covered in the examples because itll throw the uncaught which would generally lead someone to add additional error handling anyways.
@ctalkington np, I usually had the same issues so I'm just sharing how I resolved them
In my latest code I had EACCESS errors if trying to write an archive that was not owned by the script owner. Just added the fs.access, here is the commit for reference.
@ctalkington Thank you for the tip, it worked. I was wrongly thinking that the archive component would embrace the stream errors handling when calling archive.pipe( output );.
@soyuka Thank you also for this solution, but I eventually opted to react to the error instead of checking potential error ahead.
@david-rouchet unfortunately the way things currently work archiver has no internal knowledge of what the end stream is. it only knows itself and the compression stream its working with.
stream errors only bubble up iirc so the end stream is above archiver and archiver wouldnt see its errors plus theres a gap between creating stream and using it with archiver which could cause uncaughts even if it did.
the only way around this is to make a helper for common fs usage but im not sure i want archiver to take over that responsibility..