Skip to content

Commit

Permalink
fix: provide useful stacktrace on chunk loading failure
Browse files Browse the repository at this point in the history
Error created from load/error event handler has no useful stack trace
whatsoever as it's an async event and stack has unwound already at that
point.

To have better (sync) stacktrace of what triggered loading of the
chunk, create error before stack has unwound and use it and its
stacktrace later, in case of an error.

This potentially has some processing overhead as browser needs to
create a stacktrace. It's probably not that big though but I haven't
done any real testing.
  • Loading branch information
rchl committed May 19, 2019
1 parent 5178aa9 commit b322070
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 60 deletions.
4 changes: 3 additions & 1 deletion lib/web/JsonpMainTemplatePlugin.js
Expand Up @@ -173,6 +173,8 @@ class JsonpMainTemplatePlugin {
"}"
])
: "",
"// create error before stack unwound to get useful stacktrace later",
"var error = new Error();",
"onScriptComplete = function (event) {",
Template.indent([
"// avoid mem leaks in IE.",
Expand All @@ -185,7 +187,7 @@ class JsonpMainTemplatePlugin {
Template.indent([
"var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
"var realSrc = event && event.target && event.target.src;",
"var error = new Error('Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')');",
"error.message = 'Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')';",
"error.type = errorType;",
"error.request = realSrc;",
"chunk[1](error);"
Expand Down

0 comments on commit b322070

Please sign in to comment.