Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: polyfill.js

Issue 29582713: Issue 4579 - Wrap rejection reason in Error object (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Oct. 19, 2017, 12:29 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: polyfill.js
===================================================================
--- a/polyfill.js
+++ b/polyfill.js
@@ -55,16 +55,18 @@
if (!object)
return;
}
let func = object[name];
object[name] = function(...args)
{
+ let callStack = new Error().stack;
+
if (typeof args[args.length - 1] == "function")
return func.apply(object, args);
// If the last argument is undefined, we drop it from the list assuming
// it stands for the optional callback. We must do this, because we have
// to replace it with our own callback. If we simply append our own
// callback to the list, it won't match the signature of the function and
// will cause an exception.
@@ -72,22 +74,42 @@
args.pop();
return new Promise((resolve, reject) =>
{
func.call(object, ...args, result =>
{
let error = browser.runtime.lastError;
if (error)
+ {
+ // runtime.lastError on Chrome is a plain object with only a
+ // message property.
+ if (!(error instanceof Error))
Sebastian Noack 2017/10/19 04:30:24 Why checking whether it is an error, if we know th
Manish Jethani 2017/10/19 09:35:39 It seems that only Chrome's runtime.lastError is n
Manish Jethani 2017/10/19 09:37:39 Ollie just confirmed on IRC that it is in fact an
+ error = new Error(error.message);
+
+ // Add a more helpful stack trace.
+ error.stack = callStack.replace(/^Error\n {4}at Object\./,
Manish Jethani 2017/10/19 00:32:13 The stack trace from the caller's side is actually
Sebastian Noack 2017/10/19 04:30:24 Not sure whether this is worth it: * Firefox does
Wladimir Palant 2017/10/19 07:51:35 Making assumptions about the stack format and doin
Manish Jethani 2017/10/19 09:35:39 I think I see that this was a bit of overkill. I'v
+ "Error\n at browser.");
reject(error);
+ }
else
+ {
resolve(result);
+ }
});
});
};
+
+ // Set the name for debugging.
+ Object.defineProperty(object[name], "name", {
Manish Jethani 2017/10/19 00:32:13 If we do this we get the name of the API in the st
Sebastian Noack 2017/10/19 04:30:24 IMO this goes to far. Not even the original chrome
Manish Jethani 2017/10/19 09:35:39 Yes, but when the call throws an error synchronous
+ writable: false,
+ enumerable: false,
+ configurable: true,
+ value: api
+ });
}
function shouldWrapAPIs()
{
try
{
return !(browser.storage.local.get([]) instanceof Promise);
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld