Skip to content

Uncaught exceptions aren't catched or rejected #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

AndyOGo
Copy link
Contributor

@AndyOGo AndyOGo commented May 6, 2020

fixes #110

Copy link
Contributor Author

@AndyOGo AndyOGo left a comment

Choose a reason for hiding this comment

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

Please see my question.

} else if (options.imageSmoothingQuality) {
ctx.imageSmoothingQuality = options.imageSmoothingQuality
}
ctx.drawImage(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@blueimp drwaImage may throw several exceptions, like fro too huge images.

In case that happens the Promise should reject or the callback api should have an type === 'error'.
How is the best way to move this error up?

Copy link
Owner

Choose a reason for hiding this comment

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

For the Promise style API, if the result is an error, it should always reject, so your proposed solution is correct.

Copy link
Contributor Author

@AndyOGo AndyOGo left a comment

Choose a reason for hiding this comment

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

@blueimp Please see my fix for uncaught exceptions

js/load-image.js Outdated
@@ -45,6 +45,9 @@
// Not using Promises
if (resolve) resolve(img, data)
return
} else if (img instanceof Error) {
reject(img);
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@blueimp reject was never called here

Copy link
Owner

Choose a reason for hiding this comment

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

I did not consider exceptions in the transformation call previously, so that's a necessary addition now that exceptions can bubble up through the resoleWrapper as well.

Comment on lines +165 to +173

try {
loadImage.transform(img, options, callback, file, {
originalWidth: img.naturalWidth || img.width,
originalHeight: img.naturalHeight || img.height
})
} catch (error) {
callback(error)
}
Copy link
Contributor Author

@AndyOGo AndyOGo May 6, 2020

Choose a reason for hiding this comment

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

@blueimp This should now hanlde any uncaught exceptions in any transformations, if I understood the system correctly 🤔

Edit: Meanwhile I made a local overwrite and it catches all uncaught exceptions.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, that's the right location for the try...catch wrapper. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great. Any tests you would like to be added?

Copy link
Owner

Choose a reason for hiding this comment

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

I think it's fine to include this with just the existing tests.

Although it would be good to test this specific scenario (huge canvas sizes) as well, the tradeoff would likely be largely increased memory requirements for running the tests.

Copy link
Owner

Choose a reason for hiding this comment

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

One other item I'm still thinking about though is that the Promise executor already has an implicit try...catch around it, so it's only necessary for the callback style.
I might add this improvement after the merge though, since it requires some restructuring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmh, I used the Promise API, but .catch() hanlder wasn't called 🤔

Copy link
Owner

Choose a reason for hiding this comment

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

You're right, the Promise executor cannot catch errors that happen asynchronously.
So everything is fine as is. :)

@AndyOGo AndyOGo changed the title fix: uncaught exceptions, still need to reject promise Uncaught exceptions aren't catched or rejected May 6, 2020
@blueimp
Copy link
Owner

blueimp commented May 7, 2020

Thanks for your report and for providing a solution @AndyOGo. :)

@blueimp
Copy link
Owner

blueimp commented May 7, 2020

One last request, please squash your commits into a single one (technically this is possible via GitHub's UI as well, but that would remove your authorship information from the commit, which I wanna avoid).

Revert "fix: uncaught exceptions, still need to reject promise"

This reverts commit 5faa5f6.

fix: catch uncaught error in scale transformation

fix: resolveWrapper never rejected errors

Revert "fix: catch uncaught error in scale transformation"

This reverts commit 8e93865.

fix: catch any transformation error

fix: prettier
@AndyOGo AndyOGo force-pushed the bugfix/drawImage-exceptions-not-handled branch from 41b4e99 to 8e25a70 Compare May 7, 2020 11:21
@AndyOGo
Copy link
Contributor Author

AndyOGo commented May 7, 2020

@blueimp Thank you for your feedback.
I sqashed them.

@blueimp
Copy link
Owner

blueimp commented May 7, 2020

Thanks again! 👍

@blueimp blueimp merged commit ef96f78 into blueimp:master May 7, 2020
@AndyOGo AndyOGo deleted the bugfix/drawImage-exceptions-not-handled branch May 7, 2020 11:27
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.

Uncaught exceptions of transformations cause stale image loading
2 participants