Broken image ignores promise's catch
Closed this issue · 2 comments
I was hashing some images in a loop using promises and a broken image caused the entire program to crash instead of being caught by the promise chain.
/node_modules/@cwasm/nsgif/index.js:38
throw new Error('Failed to decode gif image')
^
Error: Failed to decode gif image
at Object.exports.decode (/node_modules/@cwasm/nsgif/index.js:38:11)
at decodeImage (/node_modules/@canvas/image/index.js:28:16)
at imageFromBuffer (/node_modules/@canvas/image/index.js:185:29)
at /node_modules/imghash/index.js:22:15
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:61:3)
I managed to recreate the issue by making a test file with content GIF89a
to simulate a malformed GIF and running the following code, which results in the same error. Instead of ignoring the error, it still crashes.
imghash.hash("test.txt")
.then(hash => console.log)
.catch(err => {})
I did a little digging, and it seems to be occuring because imageFromBuffer
is not a true promise in that it returns the promise after doing some synchronous work (which returns the error above). I managed to get it to work properly by amending the code in index.js to this, but it's not really a nice fix (and probably needs to be added to the if statement above fs.readFile
):
return new Promise((resolve, reject) => {
if (Buffer.isBuffer(filepath)) {
return resolve(imageFromBuffer(filepath));
}
fs.readFile(filepath, (err, content) => {
if (err) return reject(err);
try {
imageFromBuffer(content).then(image => resolve(image));
} catch (err) {
reject(err);
}
});
})
This is fixed with @canvas/image
1.0.1, you should be able to run npm update @canvas/image --depth=10
to pull in the fix in your project 👍