fix: handle unhandled promise rejections (#2568)

This commit is contained in:
Niklas von Hertzen 2021-07-12 19:01:43 +08:00 committed by GitHub
parent 382853cb33
commit 4555940d0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 181 additions and 45 deletions

View File

@ -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;
}

View File

@ -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>

View File

@ -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) {

View File

@ -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, {

200
www/package-lock.json generated
View File

@ -23,7 +23,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",
@ -34,6 +34,88 @@
"typography-theme-github": "^0.16.19"
}
},
"..": {
"version": "1.1.0",
"license": "MIT",
"dependencies": {
"css-line-break": "1.1.1"
},
"devDependencies": {
"@babel/cli": "^7.4.3",
"@babel/core": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"@babel/preset-flow": "^7.0.0",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-typescript": "^8.2.1",
"@types/chai": "^4.1.7",
"@types/express": "^4.17.13",
"@types/glob": "^7.1.1",
"@types/jest": "^26.0.24",
"@types/karma": "^6.3.0",
"@types/mocha": "^8.2.3",
"@types/node": "^16.3.1",
"@types/platform": "^1.3.4",
"@types/promise-polyfill": "^6.0.3",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"appium-ios-simulator": "^3.10.0",
"babel-eslint": "^10.0.1",
"babel-loader": "^8.0.5",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-plugin-dev-expression": "^0.2.1",
"base64-arraybuffer": "0.2.0",
"body-parser": "^1.19.0",
"chai": "4.1.1",
"chromeless": "^1.5.2",
"cors": "^2.8.5",
"es6-promise": "^4.2.8",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "3.4.0",
"express": "^4.17.1",
"filenamify-url": "1.0.0",
"glob": "7.1.3",
"html2canvas-proxy": "1.0.1",
"jest": "^27.0.6",
"jquery": "^3.5.1",
"js-polyfills": "^0.1.42",
"karma": "^6.3.2",
"karma-chrome-launcher": "^3.1.0",
"karma-edge-launcher": "^0.4.2",
"karma-firefox-launcher": "^2.1.0",
"karma-ie-launcher": "^1.0.0",
"karma-junit-reporter": "^2.0.1",
"karma-mocha": "^2.0.1",
"karma-safarinative-launcher": "^1.1.0",
"karma-sauce-launcher": "^2.0.2",
"mocha": "^9.0.2",
"node-simctl": "^5.3.0",
"platform": "^1.3.6",
"prettier": "^2.3.2",
"replace-in-file": "^3.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.53.1",
"rollup-plugin-sourcemaps": "^0.6.3",
"serve-index": "^1.9.1",
"slash": "1.0.0",
"standard-version": "^8.0.2",
"ts-jest": "^27.0.3",
"ts-loader": "^8.3.0",
"ts-node": "^10.1.0",
"tslib": "^2.3.0",
"typescript": "^4.3.5",
"uglify-js": "^3.13.10",
"uglifyjs-webpack-plugin": "^2.2.0",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12",
"yargs": "^17.0.1"
},
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/@ardatan/aggregate-error": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/@ardatan/aggregate-error/-/aggregate-error-0.0.6.tgz",
@ -3692,14 +3774,6 @@
"node": ">=0.10.0"
}
},
"node_modules/base64-arraybuffer": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz",
"integrity": "sha1-c5JncZI7Whl0etZmqlzUv5xunOg=",
"engines": {
"node": ">= 0.6.0"
}
},
"node_modules/base64-js": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
@ -5512,14 +5586,6 @@
"isobject": "^3.0.1"
}
},
"node_modules/css-line-break": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/css-line-break/-/css-line-break-1.0.1.tgz",
"integrity": "sha1-GfIGOjPpX7KDG4ZEbAuAwYivRQo=",
"dependencies": {
"base64-arraybuffer": "^0.1.5"
}
},
"node_modules/css-loader": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/css-loader/-/css-loader-1.0.1.tgz",
@ -12365,15 +12431,8 @@
"integrity": "sha512-SaGhCDPXJVNrQyKMtKy24q6IMdXg5FCPN3z+xizxw9l+oXQw5fOoaj/ERU5KqWhSYhXtW5bWthlDbTDLBhJQrA=="
},
"node_modules/html2canvas": {
"version": "1.0.0-alpha.12",
"resolved": "https://registry.npmjs.org/html2canvas/-/html2canvas-1.0.0-alpha.12.tgz",
"integrity": "sha1-OxmS48mz9WBjw1/WIElPN+uohRM=",
"dependencies": {
"css-line-break": "1.0.1"
},
"engines": {
"node": ">=4.0.0"
}
"resolved": "..",
"link": true
},
"node_modules/htmlparser2": {
"version": "3.10.1",
@ -26395,11 +26454,6 @@
}
}
},
"base64-arraybuffer": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz",
"integrity": "sha1-c5JncZI7Whl0etZmqlzUv5xunOg="
},
"base64-js": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
@ -27888,14 +27942,6 @@
"isobject": "^3.0.1"
}
},
"css-line-break": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/css-line-break/-/css-line-break-1.0.1.tgz",
"integrity": "sha1-GfIGOjPpX7KDG4ZEbAuAwYivRQo=",
"requires": {
"base64-arraybuffer": "^0.1.5"
}
},
"css-loader": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/css-loader/-/css-loader-1.0.1.tgz",
@ -33223,11 +33269,79 @@
"integrity": "sha512-SaGhCDPXJVNrQyKMtKy24q6IMdXg5FCPN3z+xizxw9l+oXQw5fOoaj/ERU5KqWhSYhXtW5bWthlDbTDLBhJQrA=="
},
"html2canvas": {
"version": "1.0.0-alpha.12",
"resolved": "https://registry.npmjs.org/html2canvas/-/html2canvas-1.0.0-alpha.12.tgz",
"integrity": "sha1-OxmS48mz9WBjw1/WIElPN+uohRM=",
"version": "file:..",
"requires": {
"css-line-break": "1.0.1"
"@babel/cli": "^7.4.3",
"@babel/core": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"@babel/preset-flow": "^7.0.0",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-typescript": "^8.2.1",
"@types/chai": "^4.1.7",
"@types/express": "^4.17.13",
"@types/glob": "^7.1.1",
"@types/jest": "^26.0.24",
"@types/karma": "^6.3.0",
"@types/mocha": "^8.2.3",
"@types/node": "^16.3.1",
"@types/platform": "^1.3.4",
"@types/promise-polyfill": "^6.0.3",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"appium-ios-simulator": "^3.10.0",
"babel-eslint": "^10.0.1",
"babel-loader": "^8.0.5",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-plugin-dev-expression": "^0.2.1",
"base64-arraybuffer": "0.2.0",
"body-parser": "^1.19.0",
"chai": "4.1.1",
"chromeless": "^1.5.2",
"cors": "^2.8.5",
"css-line-break": "1.1.1",
"es6-promise": "^4.2.8",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "3.4.0",
"express": "^4.17.1",
"filenamify-url": "1.0.0",
"glob": "7.1.3",
"html2canvas-proxy": "1.0.1",
"jest": "^27.0.6",
"jquery": "^3.5.1",
"js-polyfills": "^0.1.42",
"karma": "^6.3.2",
"karma-chrome-launcher": "^3.1.0",
"karma-edge-launcher": "^0.4.2",
"karma-firefox-launcher": "^2.1.0",
"karma-ie-launcher": "^1.0.0",
"karma-junit-reporter": "^2.0.1",
"karma-mocha": "^2.0.1",
"karma-safarinative-launcher": "^1.1.0",
"karma-sauce-launcher": "^2.0.2",
"mocha": "^9.0.2",
"node-simctl": "^5.3.0",
"platform": "^1.3.6",
"prettier": "^2.3.2",
"replace-in-file": "^3.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.53.1",
"rollup-plugin-sourcemaps": "^0.6.3",
"serve-index": "^1.9.1",
"slash": "1.0.0",
"standard-version": "^8.0.2",
"ts-jest": "^27.0.3",
"ts-loader": "^8.3.0",
"ts-node": "^10.1.0",
"tslib": "^2.3.0",
"typescript": "^4.3.5",
"uglify-js": "^3.13.10",
"uglifyjs-webpack-plugin": "^2.2.0",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12",
"yargs": "^17.0.1"
}
},
"htmlparser2": {

View File

@ -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",