Skip to content

Commit

Permalink
fix: handle unhandled promise rejections (#2568)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasvh committed Jul 12, 2021
1 parent 382853c commit 4555940
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 46 deletions.
4 changes: 3 additions & 1 deletion src/core/cache-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ export class Cache {
}

if (isBlobImage(src) || isRenderable(src)) {
this._cache[src] = this.loadImage(src);
(this._cache[src] = this.loadImage(src)).catch(() => {
// prevent unhandled rejection
});
return result;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/reftests/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
<html>
<head>
<title>iframe test</title>
<script>
h2cOptions = {useCORS: true, proxy: null};
</script>
<script type="text/javascript" src="../test.js"></script>
</head>
<body>
<div style="background: cornflowerblue; padding: 20px; width: 200px;">Parent document content</div>
<iframe src="/tests/assets/iframe/frame1.html" width="500" height="500"></iframe>
<iframe
title="Github"
src="https://ghbtns.com/github-btn.html?user=niklasvh&repo=html2canvas&type=star&count=true&size=large"
frameBorder="0"
scrolling="0"
width="160px"
height="30px"
></iframe>
</body>
</html>
4 changes: 4 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ var REFTEST = window.location.search.indexOf('reftest') !== -1;
])
.forEach(appendScript);

window.addEventListener("unhandledrejection", function(event) {
console.info('UNHANDLED PROMISE REJECTION:', event);
});

window.onload = function() {
(function($) {
$.fn.html2canvas = function(options) {
Expand Down
5 changes: 5 additions & 0 deletions tests/testrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ testList
throw new Error('Window not found for iframe');
}

contentWindow.addEventListener('unhandledrejection', (event) => {
console.error(event.reason);
throw new Error(`unhandledrejection: ${JSON.stringify(event.reason)}`);
});

const canvas: HTMLCanvasElement = await contentWindow
// @ts-ignore
.html2canvas(contentWindow.forceElement || contentWindow.document.documentElement, {
Expand Down
202 changes: 158 additions & 44 deletions www/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"gatsby-transformer-remark": "^2.3.8",
"glamor": "^2.20.40",
"gzip-size": "^5.0.0",
"html2canvas": "^1.0.0-alpha.12",
"html2canvas": "file:../",
"mkdirp": "^0.5.1",
"prismjs": "^1.16.0",
"react": "^16.8.6",
Expand Down

0 comments on commit 4555940

Please sign in to comment.