Skip to content

Conversation

@atatVec
Copy link

@atatVec atatVec commented Jan 16, 2024

Throw error on size match error & reflect same in error message

atatVec and others added 4 commits January 16, 2024 17:25
Add try/catch to handle image size mismatch errors
Update error message to reflect mismatch error & not log non-existent diff path
revert formatting
index.js Outdated
if(e.message !== IMAGE_SIZE_ERROR) console.error(e)
reject({
errorPixelCount,
errorPixelCount: Math.abs((baseImg.data.length - tmpImg.data.length) / 4),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? 🤔 Can you explain this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I see, this is virtual here basically.. Can you rewrite this to make this clearer to something like this:

} catch (e) {
  if(e.message === IMAGE_SIZE_ERROR) {
    return reject(...);
  }
  reject(e); 
}

Currently this would swallow other errors, I think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image.data.length is the number of pixels x 4 (for RGBA channels); within the version of Pixelmatch this is using, it verifies that the image data is exactly width x height x 4.
Taking the absolute value will return a positive count of differing pixels, regardless of which image is smaller.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change around rejection is sound, and I think the error should be logged so the user can see what unexpected behaviour is there

data.fullDiffPath = diffPath? path.join(__dirname, diffPath) : null;

if(reason.diffPath === null){
data.error = `Image sizes do not match ${errorPixelCount} pixels differ - img: ${tmpPath}`;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data.error = `Image sizes do not match ${errorPixelCount} pixels differ - img: ${tmpPath}`;
data.error = `Image sizes do not match - ${errorPixelCount} pixels differ - img: ${tmpPath}`;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just copying the formatting of the existing error message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants