Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #2 from TheSavior/fix/error-loading
Browse files Browse the repository at this point in the history
Fixing an issue where the error handler was not called on nonexistent images
  • Loading branch information
marcelerz committed Mar 16, 2015
2 parents 478b652 + df2348f commit 20790d6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ PNGImage.readImage = function (filename, fn) {
fn = fn || function () {
};

fs.createReadStream(filename).pipe(image).once('parsed', function () {
fs.createReadStream(filename).once('error', function(err) {
fn(err, undefined);
}).pipe(image).once('parsed', function () {
image.removeListener('error', fn);
fn(undefined, resultImage);
}).once('error', function (err) {
image.removeListener('parsed', fn);
fn(err, resultImage);
});
}).pipe(image);

return resultImage;
};
Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@ describe('Instance', function () {
}.bind(this));
});

it('should error on non-existent image', function (done) {
PNGImage.readImage(__dirname + '/nonexistent.png', function (err, image) {
expect(err).to.not.be.undefined;
done();
});
});

it('should load an image', function (done) {

var contents = fs.readFileSync(__dirname + '/test.png');
Expand Down

0 comments on commit 20790d6

Please sign in to comment.